diff options
author | Sergey Mironov <grrwlf@gmail.com> | 2014-09-02 17:37:22 +0000 |
---|---|---|
committer | Sergey Mironov <grrwlf@gmail.com> | 2014-09-02 17:37:22 +0000 |
commit | a0b96f52c6a9e5b01a74c2879d1d2e8cd95eef77 (patch) | |
tree | a8913728255df2cb2a155776b26c270616f5d871 /src | |
parent | aa8113416f649538fc8817f4aa569cfa6a7ed905 (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.c | 24 |
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); |