xdump: Avoid undefined pointer arithmetic

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>
This commit is contained in:
Markus Armbruster 2014-01-27 18:56:05 +01:00
parent 002a3a3f1e
commit 55857c7b20

View file

@ -27,7 +27,7 @@
* xdump.c: Extended dumps * xdump.c: Extended dumps
* *
* Known contributors to this file: * 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 static void
xdpresc(struct xdstr *xd, char *str, size_t n) xdpresc(struct xdstr *xd, char *str, size_t n)
{ {
unsigned char *s, *e, *l; unsigned char *s, *e;
s = (unsigned char *)str; s = (unsigned char *)str;
l = s + n;
for (;;) { for (;;) {
for (e = s; for (e = s;
e < l && *e != '"' && *e != '\\' && isgraph(*e); n && *e != '"' && *e != '\\' && isgraph(*e);
++e) e++, n--)
; ;
xd->pr("%.*s", (int)(e-s), s); xd->pr("%.*s", (int)(e-s), s);
if (e < l && *e) if (!n || !*e)
xd->pr("\\%03o", *e++);
else
break; break;
xd->pr("\\%03o", *e++);
n--;
s = e; s = e;
} }
} }