Hi,

In jcl() since args.c v1.3 we strdup every string in argv before parsing
it so that w(1) shows an accurate command invocation.

But after parsing we only need to keep these string copies for the input
and output file paths.  Everything else is effectively leaked.

So, strdup the paths for the if/of operands and free the argument copy after
processing each operand.

While here, I think setting oper equal to a copy of itself is misleading, so
just strdup *argv directly.

ok?

--
Scott Cheloha

Index: bin/dd/args.c
===================================================================
RCS file: /cvs/src/bin/dd/args.c,v
retrieving revision 1.29
diff -u -p -r1.29 args.c
--- bin/dd/args.c       3 Jan 2018 19:12:20 -0000       1.29
+++ bin/dd/args.c       24 Jul 2018 18:30:47 -0000
@@ -95,9 +95,9 @@ jcl(char **argv)
 
        in.dbsz = out.dbsz = 512;
 
-       while ((oper = *++argv) != NULL) {
-               if ((oper = strdup(oper)) == NULL)
-                       errx(1, "out of memory");
+       while (*++argv != NULL) {
+               if ((oper = strdup(*argv)) == NULL)
+                       err(1, NULL);
                if ((arg = strchr(oper, '=')) == NULL)
                        errx(1, "unknown operand %s", oper);
                *arg++ = '\0';
@@ -113,6 +113,7 @@ jcl(char **argv)
                            tmp.name);
                ddflags |= ap->set;
                ap->f(arg);
+               free(oper);
        }
 
        /* Final sanity checks. */
@@ -218,8 +219,8 @@ f_ibs(char *arg)
 static void
 f_if(char *arg)
 {
-
-       in.name = arg;
+       if ((in.name = strdup(arg)) == NULL)
+               err(1, NULL);
 }
 
 static void
@@ -233,8 +234,8 @@ f_obs(char *arg)
 static void
 f_of(char *arg)
 {
-
-       out.name = arg;
+       if ((out.name = strdup(arg)) == NULL)
+               err(1, NULL);
 }
 
 static void

Reply via email to