Просмотр исходного кода

misc/stdbuf_cmdline.h: pass exec as pointer+length

ambrop7 13 лет назад
Родитель
Сommit
510f40a7d9
2 измененных файлов с 18 добавлено и 7 удалено
  1. 17 6
      misc/stdbuf_cmdline.h
  2. 1 1
      ncd/modules/net_backend_wpa_supplicant.c

+ 17 - 6
misc/stdbuf_cmdline.h

@@ -34,9 +34,11 @@
 #ifndef BADVPN_STDBUF_CMDLINE_H
 #define BADVPN_STDBUF_CMDLINE_H
 
+#include <string.h>
+
 #include <misc/debug.h>
 #include <misc/cmdline.h>
-#include <misc/concat_strings.h>
+#include <misc/balloc.h>
 
 #define STDBUF_EXEC "/usr/bin/stdbuf"
 
@@ -46,26 +48,35 @@
  * 
  * @param out {@link CmdLine} to append the result to. Note than on failure, only
  *            some part of the cmdline may have been appended.
- * @param exec path to the executable
+ * @param exec path to the executable. Must not contain nulls. 
+ * @param exec_len number of characters in exec
  * @return 1 on success, 0 on failure
  */
-static int build_stdbuf_cmdline (CmdLine *out, const char *exec) WARN_UNUSED;
+static int build_stdbuf_cmdline (CmdLine *out, const char *exec, size_t exec_len) WARN_UNUSED;
 
-int build_stdbuf_cmdline (CmdLine *out, const char *exec)
+int build_stdbuf_cmdline (CmdLine *out, const char *exec, size_t exec_len)
 {
+    ASSERT(!memchr(exec, '\0', exec_len))
+    
     if (!CmdLine_AppendMulti(out, 3, STDBUF_EXEC, "-o", "L")) {
         goto fail1;
     }
     
     if (exec[0] == '/') {
-        if (!CmdLine_Append(out, exec)) {
+        if (!CmdLine_AppendNoNull(out, exec, exec_len)) {
             goto fail1;
         }
     } else {
-        char *real_exec = concat_strings(2, "./", exec);
+        bsize_t size = bsize_add(bsize_fromsize(exec_len), bsize_fromsize(3));
+        char *real_exec = BAllocSize(size);
         if (!real_exec) {
             goto fail1;
         }
+        
+        memcpy(real_exec, "./", 2);
+        memcpy(real_exec + 2, exec, exec_len);
+        real_exec[2 + exec_len] = '\0';
+        
         int res = CmdLine_Append(out, real_exec);
         free(real_exec);
         if (!res) {

+ 1 - 1
ncd/modules/net_backend_wpa_supplicant.c

@@ -211,7 +211,7 @@ int build_cmdline (struct instance *o, CmdLine *c)
     }
     
     // append stdbuf part
-    if (!build_stdbuf_cmdline(c, o->exec)) {
+    if (!build_stdbuf_cmdline(c, o->exec, strlen(o->exec))) {
         goto fail1;
     }