diff options
author | Nick White <git@njw.me.uk> | 2012-07-13 15:46:37 +0100 |
---|---|---|
committer | Nick White <git@njw.me.uk> | 2012-07-13 15:46:37 +0100 |
commit | 8f03147275f2522fa9d5dd16ed3e875dfffe515e (patch) | |
tree | 76cb63f899af45970f8a3054053a2d3b7a5adc94 | |
parent | 57e231682732c1e3c1ce36cd0298760a853dfd64 (diff) |
Ensure memory is all freed even for bad get()s
-rw-r--r-- | util.c | 12 |
1 files changed, 9 insertions, 3 deletions
@@ -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); |