diff options
author | Kruzya <CrazyHackGUT@users.noreply.github.com> | 2018-09-15 08:55:11 +0300 |
---|---|---|
committer | Daniel Gustafsson <daniel@yesql.se> | 2018-09-17 14:07:47 +0200 |
commit | 23524bf85b887adbc513bc015c9530355967bc04 (patch) | |
tree | 0cc1999260c30e1941c72d5185e0a1ccce28e02c /docs/examples/crawler.c | |
parent | 927cb3708e29fd88dcfadb9444d0dc93dc2aa4b2 (diff) |
examples: Fix memory leaks from realloc errors
Make sure to not overwrite the reallocated pointer in realloc() calls
to avoid a memleak on memory errors.
Diffstat (limited to 'docs/examples/crawler.c')
-rw-r--r-- | docs/examples/crawler.c | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/docs/examples/crawler.c b/docs/examples/crawler.c index 0aeb86545..d8fa5a459 100644 --- a/docs/examples/crawler.c +++ b/docs/examples/crawler.c @@ -52,7 +52,13 @@ size_t grow_buffer(void *contents, size_t sz, size_t nmemb, void *ctx) { size_t realsize = sz * nmemb; memory *mem = (memory*) ctx; - mem->buf = realloc(mem->buf, mem->size + realsize); + char *ptr = realloc(mem->buf, mem->size + realsize); + if(!ptr) { + /* out of memory */ + printf("not enough memory (realloc returned NULL)\n"); + return 0; + } + mem->buf = ptr; memcpy(&(mem->buf[mem->size]), contents, realsize); mem->size += realsize; return realsize; |