tool: change some fopen failures from warnings to errors

- Error on missing input file for --data, --data-binary,
  --data-urlencode, --header, --variable, --write-out.

Prior to this change if a user of the curl tool specified an input file
for one of the above options and that file could not be opened then it
would be treated as zero length data instead of an error. For example, a
POST using `--data @filenametypo` would cause a zero length POST which
is probably not what the user intended.

Closes https://github.com/curl/curl/pull/11677
This commit is contained in:
Jay Satiro 2023-08-20 03:08:15 -04:00
parent 2fe26a7c6a
commit aacbeae7bb
4 changed files with 28 additions and 71 deletions

View file

@ -609,10 +609,10 @@ static ParameterError data_urlencode(struct GlobalConfig *global,
}
else {
file = fopen(p, "rb");
if(!file)
warnf(global,
"Couldn't read data from file \"%s\", this makes "
"an empty POST.", nextarg);
if(!file) {
errorf(global, "Failed to open %s", p);
return PARAM_READ_ERROR;
}
}
err = file2memory(&postdata, &size, file);
@ -1761,9 +1761,11 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
}
else {
file = fopen(nextarg, "rb");
if(!file)
warnf(global, "Couldn't read data from file \"%s\", this makes "
"an empty POST.", nextarg);
if(!file) {
errorf(global, "Failed to open %s", nextarg);
err = PARAM_READ_ERROR;
break;
}
}
if((subletter == 'b') || /* --data-binary */
@ -2195,8 +2197,11 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
size_t len;
bool use_stdin = !strcmp(&nextarg[1], "-");
FILE *file = use_stdin?stdin:fopen(&nextarg[1], FOPEN_READTEXT);
if(!file)
warnf(global, "Failed to open %s", &nextarg[1]);
if(!file) {
errorf(global, "Failed to open %s", &nextarg[1]);
err = PARAM_READ_ERROR;
break;
}
else {
err = file2memory(&string, &len, file);
if(!err && string) {
@ -2544,7 +2549,12 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
}
else {
fname = nextarg;
file = fopen(nextarg, FOPEN_READTEXT);
file = fopen(fname, FOPEN_READTEXT);
if(!file) {
errorf(global, "Failed to open %s", fname);
err = PARAM_READ_ERROR;
break;
}
}
Curl_safefree(config->writeout);
err = file2string(&config->writeout, file);

View file

@ -431,12 +431,14 @@ ParameterError setvariable(struct GlobalConfig *global,
file = stdin;
else {
file = fopen(line, "rb");
if(!file) {
errorf(global, "Failed to open %s", line);
return PARAM_READ_ERROR;
}
}
if(file) {
err = file2memory(&content, &clen, file);
/* in case of out of memory, this should fail the entire operation */
contalloc = TRUE;
}
err = file2memory(&content, &clen, file);
/* in case of out of memory, this should fail the entire operation */
contalloc = TRUE;
if(!use_stdin)
fclose(file);
if(err)