]> www.infradead.org Git - mtd-utils.git/commitdiff
nanddump: Refactor pretty print code into an sprintf()
authorBrian Norris <computersforpeace@gmail.com>
Mon, 29 Nov 2010 08:01:57 +0000 (00:01 -0800)
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Thu, 2 Dec 2010 03:25:33 +0000 (05:25 +0200)
A do-while loop in pretty_dump_to_buffer() can be refactored into a
single sprintf() statement. MAX() and MIN() are used to ensure that:
(1) We have at least a single space between hex and ASCII output
(2) We don't overflow the line buffer

This patch was suggested by Mike Frysinger.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
nanddump.c

index bf95e81f0232f1469c400395ca5d4734cab155c9..14a8816cc501cf1170ba9996708474cff0ef0f38 100644 (file)
@@ -252,9 +252,10 @@ static void pretty_dump_to_buffer(const unsigned char *buf, size_t len,
        if (!ascii)
                goto nil;
 
-       do {
-               linebuf[lx++] = ' ';
-       } while (lx < (linebuflen - 1) && lx < (ascii_column - 1));
+       /* Spacing between hex and ASCII - ensure at least one space */
+       lx += sprintf(linebuf + lx, "%*s",
+                       MAX((int)MIN(linebuflen, ascii_column) - 1 - lx, 1),
+                       " ");
 
        linebuf[lx++] = '|';
        for (j = 0; (j < len) && (lx + 2) < linebuflen; j++)