diff options
author | Gilles Vollant <vollant.g@gmail.com> | 2019-09-12 10:09:22 +0200 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2019-09-12 10:10:09 +0200 |
commit | 387373f167fabfd217c01dd16ec391cb8432f960 (patch) | |
tree | a11991bfd7dc98915cabf8d6fa2d7f35219dd7a7 | |
parent | f83b2f1ae1550ee490723182f2441ee4dfa5d725 (diff) | |
download | curl-387373f167fabfd217c01dd16ec391cb8432f960.tar.gz |
curl:file2string: load large files much faster
... by using a more efficient realloc scheme.
Bug: https://curl.haxx.se/mail/lib-2019-09/0045.html
-rw-r--r-- | src/tool_paramhlp.c | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/src/tool_paramhlp.c b/src/tool_paramhlp.c index c9dac4f0f..9b6a0b1a9 100644 --- a/src/tool_paramhlp.c +++ b/src/tool_paramhlp.c @@ -59,7 +59,11 @@ struct getout *new_getout(struct OperationConfig *config) ParameterError file2string(char **bufp, FILE *file) { char *ptr; - char *string = NULL; + size_t alloc = 512; + size_t alloc_needed; + char *string = malloc(alloc); + if(!string) + return PARAM_NO_MEM; if(file) { char buffer[256]; @@ -73,12 +77,24 @@ ParameterError file2string(char **bufp, FILE *file) if(ptr) *ptr = '\0'; buflen = strlen(buffer); - ptr = realloc(string, stringlen + buflen + 1); - if(!ptr) { - Curl_safefree(string); - return PARAM_NO_MEM; + alloc_needed = stringlen + buflen + 1; + if(alloc < alloc_needed) { +#if SIZEOF_SIZE_T < 8 + if(alloc >= (size_t)SIZE_T_MAX/2) { + Curl_safefree(string); + return PARAM_NO_MEM; + } +#endif + /* doubling is enough since the string to add is always max 256 bytes + and the alloc size start at 512 */ + alloc *= 2; + ptr = realloc(string, alloc); + if(!ptr) { + Curl_safefree(string); + return PARAM_NO_MEM; + } + string = ptr; } - string = ptr; strcpy(string + stringlen, buffer); stringlen += buflen; } |