mirror of
https://github.com/curl/curl.git
synced 2026-08-24 19:23:38 +03:00
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:
parent
927cb3708e
commit
23524bf85b
5 changed files with 29 additions and 12 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue