diff options
Diffstat (limited to 'src/pack.c')
-rw-r--r-- | src/pack.c | 30 |
1 files changed, 23 insertions, 7 deletions
@@ -35,7 +35,6 @@ null padded. This letter must be followed by repeat count. c A signed char (8-bit) value. - C An unsigned char (octet) value. s A signed short (16-bit) value. S An unsigned short value. @@ -221,7 +220,7 @@ Z_packer(struct packenv *env, int rep) static void Z_unpacker(struct packenv *env, int rep) { - fprintf(env->fp, "%-*.*s", rep, rep, env->buf_base + env->buf_pos); + fprintf(env->fp, "%-*.*s", rep-1, rep-1, env->buf_base + env->buf_pos); env->buf_pos += rep; } @@ -672,18 +671,30 @@ packcomp(const char *s, char **endp) struct packinst *head = NULL, *tail = NULL, *pi; struct packspec *ps; int rep; + int ec = 0; - while (s) { + errno = 0; + while (*s) { + if (isspace(*s)) { + ++s; + continue; + } for (ps = packspec; ps->ch; ps++) if (ps->ch == *s) break; - if (!ps->ch) + if (!ps->ch) { + ec = EINVAL; break; - if (getrep(s + 1, &s, &rep)) + } + if (getrep(s + 1, &s, &rep)) { + ec = EINVAL; break; + } pi = malloc(sizeof(*pi)); - if (!pi) - return NULL; + if (!pi) { + ec = ENOMEM; + break; + } pi->next = NULL; pi->spec = ps; pi->rep = rep; @@ -693,6 +704,11 @@ packcomp(const char *s, char **endp) head = pi; tail = pi; } + if (ec) { + packfree(head); + head = NULL; + errno = ec; + } if (endp) *endp = (char*) s; return head; |