From 893bbd745821bf71c8e6c92ad017365cf82b3514 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 11 Sep 2020 10:54:35 +0200 Subject: [PATCH] curl: make file2memory use dynbuf Closes #5952 --- src/tool_paramhlp.c | 52 ++++++++++++--------------------------------- 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/src/tool_paramhlp.c b/src/tool_paramhlp.c index f60d4f957..84d7321bf 100644 --- a/src/tool_paramhlp.c +++ b/src/tool_paramhlp.c @@ -81,52 +81,28 @@ ParameterError file2string(char **bufp, FILE *file) return PARAM_OK; } +#define MAX_FILE2MEMORY (1024*1024*1024) /* big enough ? */ + ParameterError file2memory(char **bufp, size_t *size, FILE *file) { - char *newbuf; - char *buffer = NULL; - size_t nused = 0; - if(file) { size_t nread; - size_t alloc = 512; + struct curlx_dynbuf dyn; + curlx_dyn_init(&dyn, MAX_FILE2MEMORY); do { - if(!buffer || (alloc == nused)) { - /* size_t overflow detection and avoiding huge files */ - if(alloc >= (SIZE_T_MAX/4)) { - Curl_safefree(buffer); + char buffer[4096]; + nread = fread(buffer, 1, sizeof(buffer), file); + if(nread) + if(curlx_dyn_addn(&dyn, buffer, nread)) return PARAM_NO_MEM; - } - alloc *= 2; - /* allocate an extra char, reserved space, for null termination */ - newbuf = realloc(buffer, alloc + 1); - if(!newbuf) { - Curl_safefree(buffer); - return PARAM_NO_MEM; - } - buffer = newbuf; - } - nread = fread(buffer + nused, 1, alloc-nused, file); - nused += nread; } while(nread); - /* null terminate the buffer in case it's used as a string later */ - buffer[nused] = '\0'; - /* free trailing slack space, if possible */ - if(alloc != nused) { - newbuf = realloc(buffer, nused + 1); - if(!newbuf) { - Curl_safefree(buffer); - return PARAM_NO_MEM; - } - buffer = newbuf; - } - /* discard buffer if nothing was read */ - if(!nused) { - Curl_safefree(buffer); /* no string */ - } + *size = curlx_dyn_len(&dyn); + *bufp = curlx_dyn_ptr(&dyn); + } + else { + *size = 0; + *bufp = NULL; } - *size = nused; - *bufp = buffer; return PARAM_OK; }