summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Sergey Mironov <grrwlf@gmail.com>2014-09-02 17:37:22 +0000
committerGravatar Sergey Mironov <grrwlf@gmail.com>2014-09-02 17:37:22 +0000
commita0b96f52c6a9e5b01a74c2879d1d2e8cd95eef77 (patch)
treea8913728255df2cb2a155776b26c270616f5d871 /src
parentaa8113416f649538fc8817f4aa569cfa6a7ed905 (diff)
Check realloc's return code to prevent segfault on out of memory condition (Part 3)
Diffstat (limited to 'src')
-rw-r--r--src/c/http.c24
1 files changed, 19 insertions, 5 deletions
diff --git a/src/c/http.c b/src/c/http.c
index 2e419f05..2a8b7e94 100644
--- a/src/c/http.c
+++ b/src/c/http.c
@@ -97,8 +97,15 @@ static void *worker(void *data) {
if (back - buf == buf_size - 1) {
char *new_buf;
- buf_size *= 2;
- new_buf = realloc(buf, buf_size);
+ size_t new_buf_size = buf_size*2;
+ new_buf = realloc(buf, new_buf_size);
+ if(!new_buf) {
+ qfprintf(stderr, "Realloc failed while receiving header\n");
+ close(sock);
+ sock = 0;
+ break;
+ }
+ buf_size = new_buf_size;
back = new_buf + (back - buf);
buf = new_buf;
}
@@ -146,9 +153,16 @@ static void *worker(void *data) {
while (back - body < clen) {
if (back - buf == buf_size - 1) {
char *new_buf;
- buf_size *= 2;
- new_buf = realloc(buf, buf_size);
-
+ size_t new_buf_size = buf_size * 2;
+ new_buf = realloc(buf, new_buf_size);
+ if(!new_buf) {
+ qfprintf(stderr, "Realloc failed while receiving content\n");
+ close(sock);
+ sock = 0;
+ goto done;
+ }
+
+ buf_size = new_buf_size;
back = new_buf + (back - buf);
body = new_buf + (body - buf);
s = new_buf + (s - buf);