diff options
author | 2014-09-02 17:37:22 +0000 | |
---|---|---|
committer | 2014-09-02 17:37:22 +0000 | |
commit | af2946386863bcb273aa2677a3d6e235d1660b16 (patch) | |
tree | a8913728255df2cb2a155776b26c270616f5d871 /src/c/http.c | |
parent | c833454d70e5ae0436ed495f886209f95e07329e (diff) |
Check realloc's return code to prevent segfault on out of memory condition (Part 3)
Diffstat (limited to 'src/c/http.c')
-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); |