From 23524bf85b887adbc513bc015c9530355967bc04 Mon Sep 17 00:00:00 2001 From: Kruzya Date: Sat, 15 Sep 2018 08:55:11 +0300 Subject: 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. --- docs/examples/crawler.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'docs/examples/crawler.c') 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; -- cgit v1.2.3