Hi tech,

Is anyone interested in this diff? I was reading
hexdump(1) code and I noticed a couple of simplifications.

* In emalloc(), replace malloc/memset with calloc
* In parse.c and hexdump.c, replace malloc with emalloc
* Side effect: in parse.c we don't need to NUL-terminate
  the strings copied, since emalloc() already zeros the
  space allocated for the string

Works for me on amd64. Comments/OK?

- Michael


Index: display.c
===================================================================
RCS file: /cvs/src/usr.bin/hexdump/display.c,v
retrieving revision 1.20
diff -u -r1.20 display.c
--- display.c   22 Oct 2010 14:04:24 -0000      1.20
+++ display.c   10 Jun 2012 08:25:59 -0000
@@ -353,9 +353,8 @@
 {
        void *p;
 
-       if ((p = malloc((u_int)allocsize)) == NULL)
+       if ((p = calloc(1, (u_int)allocsize)) == NULL)
                nomem();
-       memset(p, 0, allocsize);
        return(p);
 }
 
Index: hexdump.c
===================================================================
RCS file: /cvs/src/usr.bin/hexdump/hexdump.c,v
retrieving revision 1.16
diff -u -r1.16 hexdump.c
--- hexdump.c   22 Sep 2011 09:09:42 -0000      1.16
+++ hexdump.c   10 Jun 2012 08:25:59 -0000
@@ -65,8 +65,7 @@
        }
        if (length != -1) {
                iobufsiz = MIN(length, blocksize);
-               if ((iobuf = malloc(iobufsiz)) == NULL)
-                       err(1, NULL);
+               iobuf = emalloc(iobufsiz);
        }
        /* rewrite the rules, do syntax checking */
        for (tfs = fshead; tfs; tfs = tfs->nextfs)
Index: parse.c
===================================================================
RCS file: /cvs/src/usr.bin/hexdump/parse.c,v
retrieving revision 1.17
diff -u -r1.17 parse.c
--- parse.c     27 Oct 2009 23:59:39 -0000      1.17
+++ parse.c     10 Jun 2012 08:25:59 -0000
@@ -60,11 +60,9 @@
                if (buf[len - 1] == '\n')
                        buf[len - 1] = '\0';
                else {
-                       /* EOF without EOL, copy and add the NUL */
-                       if ((lbuf = malloc(len + 1)) == NULL)
-                               err(1, NULL);
+                       /* EOF without EOL */
+                       lbuf = emalloc(len + 1);
                        memcpy(lbuf, buf, len);
-                       lbuf[len] = '\0';
                        buf = lbuf;
                }
                for (p = buf; isspace((unsigned char)*p); ++p);
@@ -139,10 +137,8 @@
                for (savep = ++p; *p != '"';)
                        if (*p++ == 0)
                                badfmt(fmt);
-               if (!(tfu->fmt = malloc(p - savep + 1)))
-                       nomem();
+               tfu->fmt = emalloc(p - savep + 1);
                (void) strncpy(tfu->fmt, savep, p - savep);
-               tfu->fmt[p - savep] = '\0';
                escape(tfu->fmt);
                p++;
        }

Reply via email to