summaryrefslogtreecommitdiffstats
path: root/stream/cookies.c
diff options
context:
space:
mode:
Diffstat (limited to 'stream/cookies.c')
-rw-r--r--stream/cookies.c25
1 files changed, 15 insertions, 10 deletions
diff --git a/stream/cookies.c b/stream/cookies.c
index 2680bf1834..62025b157b 100644
--- a/stream/cookies.c
+++ b/stream/cookies.c
@@ -112,44 +112,49 @@ static int parse_line(char **ptr, char *cols[6])
/* Loads a file into RAM */
static char *load_file(const char *filename, off_t * length)
{
- int fd;
- char *buffer;
+ int fd = -1;
+ char *buffer = NULL;
mp_msg(MSGT_NETWORK, MSGL_V, "Loading cookie file: %s\n", filename);
fd = open(filename, O_RDONLY);
if (fd < 0) {
mp_msg(MSGT_NETWORK, MSGL_V, "Could not open");
- return NULL;
+ goto err_out;
}
*length = lseek(fd, 0, SEEK_END);
if (*length < 0) {
mp_msg(MSGT_NETWORK, MSGL_V, "Could not find EOF");
- return NULL;
+ goto err_out;
}
if (*length > SIZE_MAX - 1) {
mp_msg(MSGT_NETWORK, MSGL_V, "File too big, could not malloc.");
- return NULL;
+ goto err_out;
}
lseek(fd, 0, SEEK_SET);
if (!(buffer = malloc(*length + 1))) {
mp_msg(MSGT_NETWORK, MSGL_V, "Could not malloc.");
- return NULL;
+ goto err_out;
}
if (read(fd, buffer, *length) != *length) {
mp_msg(MSGT_NETWORK, MSGL_V, "Read is behaving funny.");
- return NULL;
+ goto err_out;
}
close(fd);
buffer[*length] = 0;
return buffer;
+
+err_out:
+ if (fd != -1) close(fd);
+ free(buffer);
+ return NULL;
}
/* Loads a cookies.txt file into a linked list. */
@@ -164,7 +169,7 @@ static struct cookie_list_type *load_cookies_from(const char *filename,
if (!ptr)
return list;
- while (*ptr > 0) {
+ while (*ptr) {
char *cols[7];
if (parse_line(&ptr, cols)) {
struct cookie_list_type *new;
@@ -178,6 +183,7 @@ static struct cookie_list_type *load_cookies_from(const char *filename,
list = new;
}
}
+ free(ptr);
return list;
}
@@ -253,6 +259,5 @@ cookies_set(HTTP_header_t * http_hdr, const char *domain, const char *url)
if (found_cookies)
http_set_field(http_hdr, buf);
- else
- free(buf);
+ free(buf);
}