Fixed a bug in xarg: string data was strcat'ed to a malloc'ed buffer - and
authorRobert Griebl <griebl@gmx.de>
Sat, 3 Aug 2002 14:49:03 +0000 (14:49 -0000)
committerRobert Griebl <griebl@gmx.de>
Sat, 3 Aug 2002 14:49:03 +0000 (14:49 -0000)
malloc does not clear memory by default (somehow this worked on x86, but
not on arm)

findutils/xargs.c

index c63518fe545225a0eec0912a0da308e6a214843f..bb5987ec935fd9009bd4eded6286b1c3243e437e 100644 (file)
@@ -49,12 +49,11 @@ int xargs_main(int argc, char **argv)
        } else {
                /* concatenate all the arguments passed to xargs together */
                int i;
-               int len = 1; /* for the '\0' */
-               cmd_to_be_executed = xmalloc(80);
+               int len = 0;
+               for (i = 1; i < argc; i++) 
+                       len += ( strlen(argv[i]) + 1 );
+               cmd_to_be_executed = xstrndup ( "", len );      
                for (i = 1; i < argc; i++) {
-                       len += strlen(argv[i]);
-                       len += 1;  /* for the space between the args */
-                       cmd_to_be_executed = xrealloc(cmd_to_be_executed, len);
                        strcat(cmd_to_be_executed, argv[i]);
                        strcat(cmd_to_be_executed, " ");
                }
@@ -76,10 +75,9 @@ int xargs_main(int argc, char **argv)
                        continue;
 
                /* assemble the command and execute it */
-               execstr = xcalloc(strlen(cmd_to_be_executed) +
-                               strlen(file_to_act_on) + 1, sizeof(char));
-               strcat(execstr, cmd_to_be_executed);
+               execstr = xstrndup ( cmd_to_be_executed, xstrlen(cmd_to_be_executed) + xstrlen(file_to_act_on));
                strcat(execstr, file_to_act_on);
+               
                cmd_output = popen(execstr, "r");
                if (cmd_output == NULL)
                        perror_msg_and_die("popen");