curl: make file2string use dynbuf

Closes #5952
This commit is contained in:
Daniel Stenberg 2020-09-11 10:49:24 +02:00
parent da5ae38db0
commit 0938f828bf
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2

View File

@ -34,6 +34,7 @@
#include "tool_msgs.h" #include "tool_msgs.h"
#include "tool_paramhlp.h" #include "tool_paramhlp.h"
#include "tool_version.h" #include "tool_version.h"
#include "dynbuf.h"
#include "memdebug.h" /* keep this as LAST include */ #include "memdebug.h" /* keep this as LAST include */
@ -56,51 +57,27 @@ struct getout *new_getout(struct OperationConfig *config)
return node; return node;
} }
#define MAX_FILE2STRING (256*1024*1024) /* big enough ? */
ParameterError file2string(char **bufp, FILE *file) ParameterError file2string(char **bufp, FILE *file)
{ {
char *string = NULL; struct curlx_dynbuf dyn;
curlx_dyn_init(&dyn, MAX_FILE2STRING);
if(file) { if(file) {
char *ptr;
size_t alloc = 512;
size_t alloc_needed;
char buffer[256]; char buffer[256];
size_t stringlen = 0;
string = calloc(1, alloc);
if(!string)
return PARAM_NO_MEM;
while(fgets(buffer, sizeof(buffer), file)) { while(fgets(buffer, sizeof(buffer), file)) {
size_t buflen; char *ptr = strchr(buffer, '\r');
ptr = strchr(buffer, '\r');
if(ptr) if(ptr)
*ptr = '\0'; *ptr = '\0';
ptr = strchr(buffer, '\n'); ptr = strchr(buffer, '\n');
if(ptr) if(ptr)
*ptr = '\0'; *ptr = '\0';
buflen = strlen(buffer); if(curlx_dyn_add(&dyn, buffer))
alloc_needed = stringlen + buflen + 1; return PARAM_NO_MEM;
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;
}
strcpy(string + stringlen, buffer);
stringlen += buflen;
} }
} }
*bufp = string; *bufp = curlx_dyn_ptr(&dyn);
return PARAM_OK; return PARAM_OK;
} }