]> git.pond.sub.org Git - empserver/commitdiff
xdump: Avoid undefined pointer arithmetic
authorMarkus Armbruster <armbru@pond.sub.org>
Mon, 27 Jan 2014 17:56:05 +0000 (18:56 +0100)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 1 Feb 2015 15:52:58 +0000 (16:52 +0100)
s + n is defined only as long as it points into or just beyond the
same array as s.  Thus, our use of INT_MAX for "unlimited" relies on
undefined behavior.  Works fine in practice, but avoiding undefined
pointer arithmetic isn't hard here, so let's do it.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
src/lib/common/xdump.c

index 6c1cb04a2e5e6dd69faeaa3491669a07eb6054ba..0809d084c78240104a4b2852007536c32b90469b 100644 (file)
@@ -27,7 +27,7 @@
  *  xdump.c: Extended dumps
  *
  *  Known contributors to this file:
- *     Markus Armbruster, 2004-2013
+ *     Markus Armbruster, 2004-2014
  */
 
 /*
@@ -122,20 +122,19 @@ xdeval(struct valstr *val, struct xdstr *xd,
 static void
 xdpresc(struct xdstr *xd, char *str, size_t n)
 {
-    unsigned char *s, *e, *l;
+    unsigned char *s, *e;
 
     s = (unsigned char *)str;
-    l = s + n;
     for (;;) {
        for (e = s;
-            e < l && *e != '"' && *e != '\\' && isgraph(*e);
-            ++e)
+            n && *e != '"' && *e != '\\' && isgraph(*e);
+            e++, n--)
            ;
        xd->pr("%.*s", (int)(e-s), s);
-       if (e < l && *e)
-           xd->pr("\\%03o", *e++);
-       else
+       if (!n || !*e)
            break;
+       xd->pr("\\%03o", *e++);
+       n--;
        s = e;
     }
 }