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.
This commit is contained in:
Kruzya 2018-09-15 08:55:11 +03:00 committed by Daniel Gustafsson
parent 927cb3708e
commit 23524bf85b
5 changed files with 29 additions and 12 deletions

View file

@ -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;