From 8f03147275f2522fa9d5dd16ed3e875dfffe515e Mon Sep 17 00:00:00 2001 From: Nick White Date: Fri, 13 Jul 2012 15:46:37 +0100 Subject: Ensure memory is all freed even for bad get()s --- util.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/util.c b/util.c index b263f9f..2d573b0 100644 --- a/util.c +++ b/util.c @@ -70,14 +70,18 @@ int request(char *host, char *request, char *savecookie, char **body) { buf = realloc(buf, sizeof(char *) * (l+BUFSIZ)); /* strstr to find end of header */ - if((headpos = strstr(buf, "\r\n\r\n")) == NULL) + if((headpos = strstr(buf, "\r\n\r\n")) == NULL) { + free(buf); return 0; + } headpos += 4; headsize = headpos - buf; /* memcopy from there into a large enough buf */ - if((*body = malloc(sizeof(char *) * (l - headsize))) == NULL) + if((*body = malloc(sizeof(char *) * (l - headsize))) == NULL) { + free(buf); return 0; + } memcpy(*body, headpos, sizeof(char *) * (l - headsize)); /* parse header as needed */ @@ -91,6 +95,8 @@ int request(char *host, char *request, char *savecookie, char **body) { if(sscanf(headline, "HTTP/%d.%d %d", &i, &i, &p) == 3 && p != 200) { if(p == 403) fprintf(stderr, "403 forbidden: your IP address may be temporarily blocked\n"); + free(buf); + free(*body); return 0; } @@ -169,7 +175,7 @@ int renameifjpg(char *path) { fclose(f); return 1; } - strncpy(newpath, path, strlen(path)); + strncpy(newpath, path, strlen(path)+1); c = strrchr(newpath, '.'); strncpy(c+1, "jpg\0", 4); -- cgit v1.2.3