build: avoid overriding system symbols for fopen functions

By introducing wrappers for them in the curlx namespace:
`curlx_fopen()`, `curlx_fdopen()`, `curlx_fclose()`.

The undefine/redefine/`(function)()` methods broke on systems
implementing these functions as macros. E.g. AIX 32-bit's `fopen()`.

Also:
- rename `lib/fopen.*` to `lib/curl_fopen.*` (for `Curl_fopen()`)
  to make room for the newly added `curlx/fopen.h`.
- curlx: move file-related functions from `multibyte.c` to `fopen.c`.
- tests/server: stop using the curl-specific `fopen()` implementation
  on Windows. Unicode isn't used by runtests, and it isn't critical to
  run tests on longs path. It can be re-enabled if this becomes
  necessary, or if the wrapper receives a feature that's critical for
  test servers.

Reported-by: Andrew Kirillov
Bug: https://github.com/curl/curl/issues/18510#issuecomment-3274393640

Follow-up to bf7375ecc5 #18503
Follow-up to 9863599d69 #18502
Follow-up to 3bb5e58c10 #17827

Closes #18634
This commit is contained in:
Viktor Szakats 2025-09-14 15:34:18 +02:00
parent 10bac43b87
commit 20142f5d06
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
65 changed files with 568 additions and 484 deletions

View file

@ -36,6 +36,7 @@ CURLX_CFILES = \
../lib/curlx/base64.c \
../lib/curlx/multibyte.c \
../lib/curlx/dynbuf.c \
../lib/curlx/fopen.c \
../lib/curlx/nonblock.c \
../lib/curlx/strparse.c \
../lib/curlx/timediff.c \
@ -49,6 +50,7 @@ CURLX_HFILES = \
../lib/curlx/multibyte.h \
../lib/curl_setup.h \
../lib/curlx/dynbuf.h \
../lib/curlx/fopen.h \
../lib/curlx/nonblock.h \
../lib/curlx/strparse.h \
../lib/curlx/timediff.h \

View file

@ -124,7 +124,7 @@ int tool_debug_cb(CURL *handle, curl_infotype type,
/* Ok, this is somewhat hackish but we do it undocumented for now */
global->trace_stream = tool_stderr;
else {
global->trace_stream = fopen(global->trace_dump, FOPEN_WRITETEXT);
global->trace_stream = curlx_fopen(global->trace_dump, FOPEN_WRITETEXT);
global->trace_fopened = TRUE;
}
}

View file

@ -55,7 +55,7 @@ bool tool_create_output_file(struct OutStruct *outs,
(config->file_clobber_mode == CLOBBER_DEFAULT &&
!outs->is_cd_filename)) {
/* open file for writing */
file = fopen(fname, "wb");
file = curlx_fopen(fname, "wb");
}
else {
int fd;
@ -92,7 +92,7 @@ bool tool_create_output_file(struct OutStruct *outs,
is not needed because we would have failed earlier, in the while loop
and `fd` would now be -1 */
if(fd != -1) {
file = fdopen(fd, "wb");
file = curlx_fdopen(fd, "wb");
if(!file)
close(fd);
}

View file

@ -258,7 +258,7 @@ static void free_globalconfig(void)
tool_safefree(global->trace_dump);
if(global->trace_fopened && global->trace_stream)
fclose(global->trace_stream);
curlx_fclose(global->trace_stream);
global->trace_stream = NULL;
tool_safefree(global->libcurl);

View file

@ -178,7 +178,7 @@ void dumpeasysrc(void)
FILE *out;
bool fopened = FALSE;
if(strcmp(o, "-")) {
out = fopen(o, FOPEN_WRITETEXT);
out = curlx_fopen(o, FOPEN_WRITETEXT);
fopened = TRUE;
}
else
@ -227,7 +227,7 @@ void dumpeasysrc(void)
fprintf(out, "%s\n", c);
if(fopened)
fclose(out);
curlx_fclose(out);
}
easysrc_free();

View file

@ -563,14 +563,14 @@ static int get_param_part(char endchar,
endpos--;
sep = *p;
*endpos = '\0';
fp = fopen(hdrfile, FOPEN_READTEXT);
fp = curlx_fopen(hdrfile, FOPEN_READTEXT);
if(!fp)
warnf("Cannot read from %s: %s", hdrfile,
strerror(errno));
else {
int i = read_field_headers(hdrfile, fp, &headers);
fclose(fp);
curlx_fclose(fp);
if(i) {
curl_slist_free_all(headers);
return -1;

View file

@ -627,7 +627,7 @@ static ParameterError data_urlencode(const char *nextarg,
CURLX_SET_BINMODE(stdin);
}
else {
file = fopen(p, "rb");
file = curlx_fopen(p, "rb");
if(!file) {
errorf("Failed to open %s", p);
return PARAM_READ_ERROR;
@ -637,7 +637,7 @@ static ParameterError data_urlencode(const char *nextarg,
err = file2memory(&postdata, &size, file);
if(file && (file != stdin))
fclose(file);
curlx_fclose(file);
if(err)
return err;
}
@ -899,7 +899,7 @@ static ParameterError set_data(cmdline_t cmd,
CURLX_SET_BINMODE(stdin);
}
else {
file = fopen(nextarg, "rb");
file = curlx_fopen(nextarg, "rb");
if(!file) {
errorf("Failed to open %s", nextarg);
return PARAM_READ_ERROR;
@ -917,7 +917,7 @@ static ParameterError set_data(cmdline_t cmd,
}
if(file && (file != stdin))
fclose(file);
curlx_fclose(file);
if(err)
return err;
@ -1094,7 +1094,7 @@ static ParameterError parse_url(struct OperationConfig *config,
if(fromstdin)
f = stdin;
else
f = fopen(&nextarg[1], FOPEN_READTEXT);
f = curlx_fopen(&nextarg[1], FOPEN_READTEXT);
if(f) {
curlx_dyn_init(&line, 8092);
while(my_get_line(f, &line, &error)) {
@ -1104,7 +1104,7 @@ static ParameterError parse_url(struct OperationConfig *config,
break;
}
if(!fromstdin)
fclose(f);
curlx_fclose(f);
curlx_dyn_free(&line);
if(error || err)
return PARAM_READ_ERROR;
@ -1206,7 +1206,7 @@ static ParameterError parse_ech(struct OperationConfig *config,
file = stdin;
}
else {
file = fopen(nextarg, FOPEN_READTEXT);
file = curlx_fopen(nextarg, FOPEN_READTEXT);
}
if(!file) {
warnf("Couldn't read file \"%s\" "
@ -1216,7 +1216,7 @@ static ParameterError parse_ech(struct OperationConfig *config,
}
err = file2string(&tmpcfg, file);
if(file != stdin)
fclose(file);
curlx_fclose(file);
if(err)
return err;
config->ech_config = aprintf("ecl:%s",tmpcfg);
@ -1242,7 +1242,7 @@ static ParameterError parse_header(struct OperationConfig *config,
if(nextarg[0] == '@') {
/* read many headers from a file or stdin */
bool use_stdin = !strcmp(&nextarg[1], "-");
FILE *file = use_stdin ? stdin : fopen(&nextarg[1], FOPEN_READTEXT);
FILE *file = use_stdin ? stdin : curlx_fopen(&nextarg[1], FOPEN_READTEXT);
if(!file) {
errorf("Failed to open %s", &nextarg[1]);
err = PARAM_READ_ERROR;
@ -1263,7 +1263,7 @@ static ParameterError parse_header(struct OperationConfig *config,
err = PARAM_READ_ERROR;
curlx_dyn_free(&line);
if(!use_stdin)
fclose(file);
curlx_fclose(file);
}
}
else {
@ -1536,7 +1536,7 @@ static ParameterError parse_writeout(struct OperationConfig *config,
}
else {
fname = nextarg;
file = fopen(fname, FOPEN_READTEXT);
file = curlx_fopen(fname, FOPEN_READTEXT);
if(!file) {
errorf("Failed to open %s", fname);
return PARAM_READ_ERROR;
@ -1545,7 +1545,7 @@ static ParameterError parse_writeout(struct OperationConfig *config,
tool_safefree(config->writeout);
err = file2string(&config->writeout, file);
if(file && (file != stdin))
fclose(file);
curlx_fclose(file);
if(err)
return err;
if(!config->writeout)

View file

@ -88,7 +88,7 @@ static char *ipfs_gateway(void)
if(!gateway_composed_file_path)
goto fail;
gateway_file = fopen(gateway_composed_file_path, FOPEN_READTEXT);
gateway_file = curlx_fopen(gateway_composed_file_path, FOPEN_READTEXT);
tool_safefree(gateway_composed_file_path);
if(gateway_file) {
@ -103,7 +103,7 @@ static char *ipfs_gateway(void)
goto fail;
}
fclose(gateway_file);
curlx_fclose(gateway_file);
gateway_file = NULL;
if(curlx_dyn_len(&dyn))
@ -121,7 +121,7 @@ static char *ipfs_gateway(void)
}
fail:
if(gateway_file)
fclose(gateway_file);
curlx_fclose(gateway_file);
tool_safefree(gateway);
tool_safefree(ipfs_path);
return NULL;

View file

@ -164,7 +164,7 @@ static curl_off_t vms_realfilesize(const char *name,
FILE * file;
/* !checksrc! disable FOPENMODE 1 */
file = fopen(name, "r"); /* VMS */
file = curlx_fopen(name, "r"); /* VMS */
if(!file) {
return 0;
}
@ -175,7 +175,7 @@ static curl_off_t vms_realfilesize(const char *name,
if(ret_stat)
count += ret_stat;
}
fclose(file);
curlx_fclose(file);
return count;
}
@ -660,7 +660,7 @@ static CURLcode post_per_transfer(struct per_transfer *per,
/* Close the outs file */
if(outs->fopened && outs->stream) {
rc = fclose(outs->stream);
rc = curlx_fclose(outs->stream);
if(!result && rc) {
/* something went wrong in the writing process */
result = CURLE_WRITE_ERROR;
@ -696,13 +696,13 @@ skip:
/* Close function-local opened file descriptors */
if(per->heads.fopened && per->heads.stream)
fclose(per->heads.stream);
curlx_fclose(per->heads.stream);
if(per->heads.alloc_filename)
tool_safefree(per->heads.filename);
if(per->etag_save.fopened && per->etag_save.stream)
fclose(per->etag_save.stream);
curlx_fclose(per->etag_save.stream);
if(per->etag_save.alloc_filename)
tool_safefree(per->etag_save.filename);
@ -800,7 +800,7 @@ static CURLcode etag_compare(struct OperationConfig *config)
ParameterError pe;
/* open file for reading: */
FILE *file = fopen(config->etag_compare_file, FOPEN_READTEXT);
FILE *file = curlx_fopen(config->etag_compare_file, FOPEN_READTEXT);
if(!file)
warnf("Failed to open %s: %s", config->etag_compare_file,
strerror(errno));
@ -815,7 +815,7 @@ static CURLcode etag_compare(struct OperationConfig *config)
if(!header) {
if(file)
fclose(file);
curlx_fclose(file);
errorf("Failed to allocate memory for custom etag header");
return CURLE_OUT_OF_MEMORY;
}
@ -825,7 +825,7 @@ static CURLcode etag_compare(struct OperationConfig *config)
tool_safefree(header);
if(file)
fclose(file);
curlx_fclose(file);
if(pe != PARAM_OK)
result = CURLE_OUT_OF_MEMORY;
return result;
@ -843,7 +843,7 @@ static CURLcode etag_store(struct OperationConfig *config,
/* open file for output: */
if(strcmp(config->etag_save_file, "-")) {
FILE *newfile = fopen(config->etag_save_file, "ab");
FILE *newfile = curlx_fopen(config->etag_save_file, "ab");
if(!newfile) {
warnf("Failed creating file for saving etags: \"%s\". "
"Skip this transfer", config->etag_save_file);
@ -893,11 +893,11 @@ static CURLcode setup_headerfile(struct OperationConfig *config,
return result;
}
if(!per->prev || per->prev->config != config) {
newfile = fopen(config->headerfile, "wb");
newfile = curlx_fopen(config->headerfile, "wb");
if(newfile)
fclose(newfile);
curlx_fclose(newfile);
}
newfile = fopen(config->headerfile, "ab");
newfile = curlx_fopen(config->headerfile, "ab");
if(!newfile) {
errorf("Failed to open %s", config->headerfile);
@ -999,11 +999,11 @@ static CURLcode setup_outfile(struct OperationConfig *config,
#ifdef __VMS
/* open file for output, forcing VMS output format into stream
mode which is needed for stat() call above to always work. */
FILE *file = fopen(outfile, "ab",
"ctx=stm", "rfm=stmlf", "rat=cr", "mrs=0");
FILE *file = curlx_fopen(outfile, "ab",
"ctx=stm", "rfm=stmlf", "rat=cr", "mrs=0");
#else
/* open file for output: */
FILE *file = fopen(per->outfile, "ab");
FILE *file = curlx_fopen(per->outfile, "ab");
#endif
if(!file) {
errorf("cannot open '%s'", per->outfile);
@ -1193,7 +1193,7 @@ static CURLcode single_transfer(struct OperationConfig *config,
if(result) {
curl_easy_cleanup(curl);
if(etag_first.fopened)
fclose(etag_first.stream);
curlx_fclose(etag_first.stream);
return result;
}
per->etag_save = etag_first; /* copy the whole struct */
@ -2004,7 +2004,7 @@ static CURLcode cacertpaths(struct OperationConfig *config)
char *cacert = NULL;
FILE *cafile = tool_execpath("curl-ca-bundle.crt", &cacert);
if(cafile) {
fclose(cafile);
curlx_fclose(cafile);
config->cacert = strdup(cacert);
}
#elif !defined(CURL_WINDOWS_UWP) && !defined(UNDER_CE) && \

View file

@ -93,7 +93,7 @@ int parseconfig(const char *filename)
/* NULL means load .curlrc from homedir! */
char *curlrc = findfile(".curlrc", CURLRC_DOTSCORE);
if(curlrc) {
file = fopen(curlrc, FOPEN_READTEXT);
file = curlx_fopen(curlrc, FOPEN_READTEXT);
if(!file) {
free(curlrc);
return 1;
@ -115,7 +115,7 @@ int parseconfig(const char *filename)
}
else {
if(strcmp(filename, "-"))
file = fopen(filename, FOPEN_READTEXT);
file = curlx_fopen(filename, FOPEN_READTEXT);
else
file = stdin;
}
@ -250,7 +250,7 @@ int parseconfig(const char *filename)
curlx_dyn_free(&buf);
curlx_dyn_free(&pbuf);
if(file != stdin)
fclose(file);
curlx_fclose(file);
if(fileerror)
rc = 1;
}

View file

@ -66,7 +66,7 @@ CURLcode tool_ssls_load(struct OperationConfig *config,
bool error = FALSE;
curlx_dyn_init(&buf, MAX_SSLS_LINE);
fp = fopen(filename, FOPEN_READTEXT);
fp = curlx_fopen(filename, FOPEN_READTEXT);
if(!fp) { /* ok if it does not exist */
notef("SSL session file does not exist (yet?): %s", filename);
goto out;
@ -122,7 +122,7 @@ out:
if(easy)
curl_easy_cleanup(easy);
if(fp)
fclose(fp);
curlx_fclose(fp);
curlx_dyn_free(&buf);
curl_free(shmac);
curl_free(sdata);
@ -190,7 +190,7 @@ CURLcode tool_ssls_save(struct OperationConfig *config,
CURLcode r = CURLE_OK;
ctx.exported = 0;
ctx.fp = fopen(filename, FOPEN_WRITETEXT);
ctx.fp = curlx_fopen(filename, FOPEN_WRITETEXT);
if(!ctx.fp) {
warnf("Warning: Failed to create SSL session file %s",
filename);
@ -207,6 +207,6 @@ out:
if(easy)
curl_easy_cleanup(easy);
if(ctx.fp)
fclose(ctx.fp);
curlx_fclose(ctx.fp);
return r;
}

View file

@ -50,12 +50,12 @@ void tool_set_stderr_file(const char *filename)
/* precheck that filename is accessible to lessen the chance that the
subsequent freopen will fail. */
fp = fopen(filename, FOPEN_WRITETEXT);
fp = curlx_fopen(filename, FOPEN_WRITETEXT);
if(!fp) {
warnf("Warning: Failed to open %s", filename);
return;
}
fclose(fp);
curlx_fclose(fp);
/* freopen the actual stderr (stdio.h stderr) instead of tool_stderr since
the latter may be set to stdout. */

View file

@ -127,7 +127,7 @@ FILE *tool_execpath(const char *filename, char **pathp)
if(strlen(filename) < remaining - 1) {
curl_msnprintf(lastdirchar, remaining, "%s%s", DIR_CHAR, filename);
*pathp = filebuffer;
return fopen(filebuffer, FOPEN_READTEXT);
return curlx_fopen(filebuffer, FOPEN_READTEXT);
}
}
}

View file

@ -772,13 +772,13 @@ void ourWriteOut(struct OperationConfig *config, struct per_transfer *per,
break;
case VAR_STDOUT:
if(fclose_stream)
fclose(stream);
curlx_fclose(stream);
fclose_stream = FALSE;
stream = stdout;
break;
case VAR_STDERR:
if(fclose_stream)
fclose(stream);
curlx_fclose(stream);
fclose_stream = FALSE;
stream = tool_stderr;
break;
@ -824,12 +824,12 @@ void ourWriteOut(struct OperationConfig *config, struct per_transfer *per,
FILE *stream2;
memcpy(fname, ptr, flen);
fname[flen] = 0;
stream2 = fopen(fname, append ? FOPEN_APPENDTEXT :
FOPEN_WRITETEXT);
stream2 = curlx_fopen(fname, append ? FOPEN_APPENDTEXT :
FOPEN_WRITETEXT);
if(stream2) {
/* only change if the open worked */
if(fclose_stream)
fclose(stream);
curlx_fclose(stream);
stream = stream2;
fclose_stream = TRUE;
}
@ -872,6 +872,6 @@ void ourWriteOut(struct OperationConfig *config, struct per_transfer *per,
}
}
if(fclose_stream)
fclose(stream);
curlx_fclose(stream);
curlx_dyn_free(&name);
}

View file

@ -455,7 +455,7 @@ ParameterError setvariable(const char *input)
if(use_stdin)
file = stdin;
else {
file = fopen(line, "rb");
file = curlx_fopen(line, "rb");
if(!file) {
errorf("Failed to open %s: %s", line, strerror(errno));
err = PARAM_READ_ERROR;
@ -469,7 +469,7 @@ ParameterError setvariable(const char *input)
}
curlx_dyn_free(&fname);
if(!use_stdin && file)
fclose(file);
curlx_fclose(file);
if(err)
return err;
}