--- libaitwww/src/mime.c 2012/03/09 15:34:37 1.1.1.1.2.2 +++ libaitwww/src/mime.c 2012/03/15 01:59:37 1.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: mime.c,v 1.1.1.1.2.2 2012/03/09 15:34:37 misho Exp $ +* $Id: mime.c,v 1.3 2012/03/15 01:59:37 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -169,8 +169,8 @@ hdrValue(const char *str, size_t len, const char **end e = str + len; while (str < e) { - if ((crlf = findtextpos(str, e - str, CRLF, strlen(CRLF)))) { - www_SetErr(EBADMSG, "Bad MIME format message"); + if (!(crlf = findtextpos(str, e - str, CRLF, strlen(CRLF)))) { + www_SetErr(EFAULT, "Bad header format of MIME part"); return NULL; } @@ -328,7 +328,7 @@ mime_parseMultiPart(const char *str, size_t len, const mime_t *mime = NULL; struct iovec bd[2]; struct tagMIME *m, *old = NULL; - const char *next; + const char *next = NULL; if (!str | !bdtag) { www_SetErr(EINVAL, "String or boundary tag is NULL"); @@ -362,7 +362,7 @@ mime_parseMultiPart(const char *str, size_t len, const /* check boundary tag */ if (memcmp(str, strstr(bd[0].iov_base, "--"), strlen(strstr(bd[0].iov_base, "--")))) { - www_SetErr(EBADMSG, "Bad content data, not found boundary tag"); + www_SetErr(EFAULT, "Bad content data, not found boundary tag"); free(bd[1].iov_base); free(bd[0].iov_base); free(mime); @@ -372,7 +372,7 @@ mime_parseMultiPart(const char *str, size_t len, const len -= strlen(strstr(bd[0].iov_base, "--")); } - while (42) { + while (len > 0) { m = malloc(sizeof(struct tagMIME)); if (!m) { LOGERR; @@ -420,6 +420,9 @@ mime_parseMultiPart(const char *str, size_t len, const len -= bd[0].iov_len; */ + free(bd[1].iov_base); + free(bd[0].iov_base); + if (end) *end = str; return mime; @@ -493,7 +496,7 @@ mime_parseHeader(struct tagMIME * __restrict m, const colon = memchr(str, ':', e - str); eoh = findtextpos(str, e - str, CRLF, strlen(CRLF)); if (!colon || !eoh || colon > eoh) { - www_SetErr(EBADMSG, "Bad MIME format message"); + www_SetErr(EFAULT, "Bad MIME format message"); freeHeader(m); return -1; } @@ -584,14 +587,21 @@ mime_readPart(struct tagMIME * __restrict m, const cha if (!ct || www_cmptype(ct, "multipart")) { /* not multi part, assign like body element */ m->mime_body.iov_base = malloc(len - (eoh - str) + 1); + if (!m->mime_body.iov_base) { + LOGERR; + freeHeader(m); + return -1; + } memcpy(m->mime_body.iov_base, eoh, len - (eoh - str)); ((char*) m->mime_body.iov_base)[len - (eoh - str)] = 0; m->mime_body.iov_len = len - (eoh - str) + 1; } else { /* multi part */ attr = www_parseAttributes(&ct); - if (!attr) + if (!attr) { + freeHeader(m); return -1; + } bd.iov_base = bd_begin(www_getAttribute(attr, "boundary")); bd.iov_len = strlen(bd.iov_base); eb = findtextpos(eoh, len - (eoh - str), bd.iov_base, bd.iov_len); @@ -603,6 +613,7 @@ mime_readPart(struct tagMIME * __restrict m, const cha if (!m->mime_prolog.iov_base) { LOGERR; www_freeAttributes(&attr); + freeHeader(m); return -1; } memcpy(m->mime_prolog.iov_base, eoh, eb - eoh); @@ -619,6 +630,7 @@ mime_readPart(struct tagMIME * __restrict m, const cha if (!m->mime_epilog.iov_base) { LOGERR; www_freeAttributes(&attr); + freeHeader(m); return -1; } memcpy(m->mime_epilog.iov_base, str, len - (eoh - str)); @@ -626,9 +638,10 @@ mime_readPart(struct tagMIME * __restrict m, const cha m->mime_epilog.iov_len = len - (eoh - str) + 1; } + + www_freeAttributes(&attr); } - www_freeAttributes(&attr); return 0; }