summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2020-09-11 10:54:35 +0200
committerDaniel Stenberg <daniel@haxx.se>2020-09-14 12:33:51 +0200
commit893bbd745821bf71c8e6c92ad017365cf82b3514 (patch)
treec985a4a2777b057caca8c8f7ea2b70ce14ae30de /src
parent0938f828bfa3c06416e6b4fb1be67340485466f6 (diff)
downloadcurl-893bbd745821bf71c8e6c92ad017365cf82b3514.tar.gz
curl: make file2memory use dynbuf
Closes #5952
Diffstat (limited to 'src')
-rw-r--r--src/tool_paramhlp.c52
1 files 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);
- 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);
+ char buffer[4096];
+ nread = fread(buffer, 1, sizeof(buffer), file);
+ if(nread)
+ if(curlx_dyn_addn(&dyn, buffer, nread))
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;
}