summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick White <git@njw.me.uk>2012-07-13 15:46:37 +0100
committerNick White <git@njw.me.uk>2012-07-13 15:46:37 +0100
commit8f03147275f2522fa9d5dd16ed3e875dfffe515e (patch)
tree76cb63f899af45970f8a3054053a2d3b7a5adc94
parent57e231682732c1e3c1ce36cd0298760a853dfd64 (diff)
Ensure memory is all freed even for bad get()s
-rw-r--r--util.c12
1 files 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);