diff --git a/src/tool_formparse.c b/src/tool_formparse.c index 844b18d154..5ea14f7641 100644 --- a/src/tool_formparse.c +++ b/src/tool_formparse.c @@ -125,21 +125,20 @@ static struct tool_mime *tool_mime_new_filedata(struct tool_mime *parent, else { /* Not suitable for direct use, buffer stdin data. */ size_t stdinsize = 0; - if(file2memory(&data, &stdinsize, stdin) != PARAM_OK) { - /* Out of memory. */ + switch(file2memory(&data, &stdinsize, stdin)) { + case PARAM_NO_MEM: return m; - } - - if(ferror(stdin)) { + case PARAM_READ_ERROR: result = CURLE_READ_ERROR; - Curl_safefree(data); - data = NULL; - } - else if(!stdinsize) { - /* Zero-length data has been freed. Re-create it. */ - data = strdup(""); - if(!data) - return m; + break; + default: + if(!stdinsize) { + /* Zero-length data has been freed. Re-create it. */ + data = strdup(""); + if(!data) + return m; + } + break; } size = curlx_uztoso(stdinsize); origin = 0; diff --git a/src/tool_getparam.h b/src/tool_getparam.h index 599a0ac885..4f578d5139 100644 --- a/src/tool_getparam.h +++ b/src/tool_getparam.h @@ -7,7 +7,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2021, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2022, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -45,6 +45,7 @@ typedef enum { PARAM_NO_NOT_BOOLEAN, PARAM_CONTDISP_SHOW_HEADER, /* --include and --remote-header-name */ PARAM_CONTDISP_RESUME_FROM, /* --continue-at and --remote-header-name */ + PARAM_READ_ERROR, PARAM_LAST } ParameterError; diff --git a/src/tool_helpers.c b/src/tool_helpers.c index d47a8d244b..f604def68d 100644 --- a/src/tool_helpers.c +++ b/src/tool_helpers.c @@ -72,6 +72,8 @@ const char *param2text(int res) return "showing headers and --remote-header-name cannot be combined"; case PARAM_CONTDISP_RESUME_FROM: return "--continue-at and --remote-header-name cannot be combined"; + case PARAM_READ_ERROR: + return "error encountered when reading a file"; default: return "unknown error"; } diff --git a/src/tool_operate.c b/src/tool_operate.c index bf6aff6eea..cb587e0a64 100644 --- a/src/tool_operate.c +++ b/src/tool_operate.c @@ -2621,6 +2621,8 @@ CURLcode operate(struct GlobalConfig *global, int argc, argv_item_t argv[]) tool_list_engines(); else if(res == PARAM_LIBCURL_UNSUPPORTED_PROTOCOL) result = CURLE_UNSUPPORTED_PROTOCOL; + else if(res == PARAM_READ_ERROR) + result = CURLE_READ_ERROR; else result = CURLE_FAILED_INIT; } diff --git a/src/tool_paramhlp.c b/src/tool_paramhlp.c index 273805ef0d..5c731980e0 100644 --- a/src/tool_paramhlp.c +++ b/src/tool_paramhlp.c @@ -94,10 +94,16 @@ ParameterError file2memory(char **bufp, size_t *size, FILE *file) do { char buffer[4096]; nread = fread(buffer, 1, sizeof(buffer), file); + if(ferror(file)) { + curlx_dyn_free(&dyn); + *size = 0; + *bufp = NULL; + return PARAM_READ_ERROR; + } if(nread) if(curlx_dyn_addn(&dyn, buffer, nread)) return PARAM_NO_MEM; - } while(nread); + } while(!feof(file)); *size = curlx_dyn_len(&dyn); *bufp = curlx_dyn_ptr(&dyn); }