fix lengthcheck in m_getfld2()
[mmh] / sbr / m_getfld2.c
index cd1c6be..74d25e4 100644 (file)
@@ -26,6 +26,8 @@ static enum threestate is_falted(FILE *);
 static size_t copyname(char *, char *);
 static bool is_separator(char *);
 
 static size_t copyname(char *, char *);
 static bool is_separator(char *);
 
+struct field free_field = { "\0", 0, NULL, 0, 0 };
+
 
 /*
 ** FLD2:       We read a (complete) header field
 
 /*
 ** FLD2:       We read a (complete) header field
@@ -55,7 +57,7 @@ m_getfld2(enum state s, struct field *f, FILE *msg)
                nchars = getline(&tmpline, &len, msg);
                if (nchars < 1) {
                        free(f->value);
                nchars = getline(&tmpline, &len, msg);
                if (nchars < 1) {
                        free(f->value);
-                       *f = (struct field) { "\0", 0, NULL, 0, 0 };
+                       *f = free_field;
                        if (feof(msg)) {
                                return FILEEOF2;
                        } else {
                        if (feof(msg)) {
                                return FILEEOF2;
                        } else {
@@ -130,15 +132,12 @@ m_getfld2(enum state s, struct field *f, FILE *msg)
                return ret;
 
        case BODY2:
                return ret;
 
        case BODY2:
-               *f->name = '\0';
-               f->namelen = 0;
+               free(f->value);
+               *f = free_field;
 
                nchars = getline(&tmpline, &len, msg);
                if (nchars < 1) {
 
                nchars = getline(&tmpline, &len, msg);
                if (nchars < 1) {
-                       free(f->value);
-                       f->value = NULL;
-                       f->valuelen = 0;
-                       f->alloclen = 0;
+                       free(tmpline);
                        if (feof(msg)) {
                                return FILEEOF2;
                        } else {
                        if (feof(msg)) {
                                return FILEEOF2;
                        } else {
@@ -150,7 +149,6 @@ m_getfld2(enum state s, struct field *f, FILE *msg)
                        ret = LENERR2;
                }
 
                        ret = LENERR2;
                }
 
-               free(f->value);
                f->value = tmpline;
                f->valuelen = nchars;
                f->alloclen = len;
                f->value = tmpline;
                f->valuelen = nchars;
                f->alloclen = len;
@@ -204,7 +202,7 @@ copyname(char *dst, char *src)
        }
 
        len = sep - src;
        }
 
        len = sep - src;
-       if (len >= NAMESZ) {
+       if (len >= NAMESZ - 1) {
                return 0;
        }
 
                return 0;
        }