]> git.pond.sub.org Git - empserver/commitdiff
xundump: Clean up lazy coding limiting string length to 65536
authorMarkus Armbruster <armbru@pond.sub.org>
Mon, 27 Jan 2014 18:44:44 +0000 (19:44 +0100)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 1 Feb 2015 15:52:58 +0000 (16:52 +0100)
Can't just replace the literal 65536 by INT_MAX, because my system's
vfprintf() rejects that with EOVERFLOW.

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

index 1f486bcb96eb9401856874ea6f1bbf646574a296..33b726804b8b73d42f90a1e1e39c02e6e3ddf9ec 100644 (file)
@@ -52,6 +52,7 @@
 #include <config.h>
 
 #include <ctype.h>
+#include <limits.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -774,7 +775,7 @@ static int
 setstr(int fldno, char *str)
 {
     struct castr *ca;
-    int must_match, idx;
+    int must_match, mismatch, idx;
     size_t sz, len;
     char *memb_ptr, *old;
 
@@ -789,14 +790,17 @@ setstr(int fldno, char *str)
     memb_ptr = cur_obj;
     memb_ptr += ca->ca_off;
     must_match = fldval_must_match(fldno);
+    mismatch = 0;
 
     switch (ca->ca_type) {
     case NSC_STRING:
        old = ((char **)memb_ptr)[idx];
-       if (!must_match)
+       if (must_match)
+           mismatch = old ? !str || strcmp(old, str) : !!str;
+       else
            /* FIXME may leak old value */
            ((char **)memb_ptr)[idx] = str ? strdup(str) : NULL;
-       len = 65535;            /* really SIZE_MAX, but that's C99 */
+       len = -1;               /* unlimited */
        break;
     case NSC_STRINGY:
        if (CANT_HAPPEN(idx))
@@ -810,18 +814,20 @@ setstr(int fldno, char *str)
            return gripe("Field %d takes at most %d characters",
                         fldno + 1, (int)len);
        old = memb_ptr;
-       if (!must_match)
+       if (must_match)
+           mismatch = !str || strncmp(old, str, len);
+       else
            strncpy(memb_ptr, str, sz);
        break;
     default:
        return gripe("Field %d doesn't take strings", fldno + 1);
     }
 
-    if (must_match) {
-       if (old && (!str || strncmp(old, str, len)))
+    if (mismatch) {
+       if (old)
            return gripe("Value for field %d must be \"%.*s\"",
                         fldno + 1, (int)len, old);
-       if (!old && str)
+       else
            return gripe("Value for field %d must be nil", fldno + 1);
     }