build: avoid overriding system open and stat symbols

Replace them by `curlx_open()` and `curlx_stat()`.

To make it obvious in the source code what is being executed.

Also:
- tests/server: stop overriding `open()` for test servers.
  This is critical for the call made from the signal handler.
  For other calls, it's an option to use `curlx_open()`, but
  doesn't look important enough to do it, following the path
  taken with `fopen()`.

Follow-up to 10bac43b87 #18774
Follow-up to 20142f5d06 #18634
Follow-up to bf7375ecc5 #18503

Closes #18776
This commit is contained in:
Viktor Szakats 2025-09-30 01:27:10 +02:00
parent 684f4cdd3e
commit 9678ff5b1b
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
31 changed files with 76 additions and 83 deletions

View File

@ -3,4 +3,5 @@ allowfunc fdopen
allowfunc fopen
allowfunc gmtime
allowfunc localtime
allowfunc open
allowfunc socket

View File

@ -104,6 +104,7 @@ int main(int argc, char **argv)
return 2;
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
stat(file, &file_info);
#else
fstat(fileno(fp), &file_info);

View File

@ -52,6 +52,7 @@ int main(void)
/* to get the file size */
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
if(stat("debugit", &file_info) != 0) {
#else
if(fstat(fileno(fd), &file_info) != 0) {

View File

@ -96,6 +96,7 @@ int main(void)
/* to get the file size */
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
if(stat(LOCAL_FILE, &file_info) != 0) {
#else
if(fstat(fileno(hd_src), &file_info) != 0) {

View File

@ -234,6 +234,7 @@ static int setup(struct input *i, int num, const char *upload)
}
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
if(stat(upload, &file_info) != 0) {
#else
if(fstat(fileno(i->in), &file_info) != 0) {

View File

@ -91,6 +91,7 @@ int main(int argc, char **argv)
/* get the file size of the local file */
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
if(stat(file, &file_info) != 0) {
#else
if(fstat(fileno(hd_src), &file_info) != 0) {

View File

@ -484,6 +484,7 @@
#define CURL_DISABLE_LDAP 1
#ifndef _MSC_VER
/* !checksrc! disable BANNEDFUNC 1 */
extern int stat(const char *path, struct stat *buffer);
#endif

View File

@ -27,10 +27,6 @@
#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \
!defined(CURL_DISABLE_HSTS)
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include "urldata.h"
#include "rand.h"
#include "curl_fopen.h"
@ -107,6 +103,7 @@ CURLcode Curl_fopen(struct Curl_easy *data, const char *filename,
goto fail;
if(
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
stat(filename, &sb) == -1
#else
fstat(fileno(*fh), &sb) == -1
@ -137,9 +134,11 @@ CURLcode Curl_fopen(struct Curl_easy *data, const char *filename,
result = CURLE_WRITE_ERROR;
#if (defined(ANDROID) || defined(__ANDROID__)) && \
(defined(__i386__) || defined(__arm__))
fd = open(tempstore, O_WRONLY | O_CREAT | O_EXCL, (mode_t)(0600|sb.st_mode));
fd = curlx_open(tempstore, O_WRONLY | O_CREAT | O_EXCL,
(mode_t)(0600 | sb.st_mode));
#else
fd = open(tempstore, O_WRONLY | O_CREAT | O_EXCL, 0600|sb.st_mode);
fd = curlx_open(tempstore, O_WRONLY | O_CREAT | O_EXCL,
0600 | sb.st_mode);
#endif
if(fd == -1)
goto fail;

View File

@ -506,12 +506,6 @@
# endif
# define LSEEK_ERROR (long)-1
# endif
# ifndef UNDER_CE
int curlx_win32_stat(const char *path, struct_stat *buffer);
int curlx_win32_open(const char *filename, int oflag, ...);
# define stat(fname, stp) curlx_win32_stat(fname, stp)
# define open curlx_win32_open
# endif
#elif defined(__DJGPP__)
/* Requires DJGPP 2.04 */
# include <unistd.h>

View File

@ -30,19 +30,28 @@
#if defined(_WIN32) && !defined(UNDER_CE)
FILE *curlx_win32_fopen(const char *filename, const char *mode);
int curlx_win32_stat(const char *path, struct_stat *buffer);
int curlx_win32_open(const char *filename, int oflag, ...);
#define CURLX_FOPEN_LOW(fname, mode) curlx_win32_fopen(fname, mode)
#define curlx_stat(fname, stp) curlx_win32_stat(fname, stp)
#define curlx_open curlx_win32_open
#else
#define CURLX_FOPEN_LOW fopen
#ifdef HAVE_FCNTL_H
#include <fcntl.h> /* for open() */
#endif
#define CURLX_FOPEN_LOW fopen
#define curlx_stat(fname, stp) stat(fname, stp)
#define curlx_open open
#endif
#ifdef CURLDEBUG
#define curlx_fopen(file,mode) curl_dbg_fopen(file,mode,__LINE__,__FILE__)
#define curlx_fopen(file,mode) curl_dbg_fopen(file,mode,__LINE__,__FILE__)
#define curlx_fdopen(file,mode) curl_dbg_fdopen(file,mode,__LINE__,__FILE__)
#define curlx_fclose(file) curl_dbg_fclose(file,__LINE__,__FILE__)
#define curlx_fclose(file) curl_dbg_fclose(file,__LINE__,__FILE__)
#else
#define curlx_fopen CURLX_FOPEN_LOW
#define curlx_fdopen fdopen
#define curlx_fclose fclose
#define curlx_fopen CURLX_FOPEN_LOW
#define curlx_fdopen fdopen
#define curlx_fclose fclose
#endif
#endif /* HEADER_CURLX_FOPEN_H */

View File

@ -46,10 +46,6 @@
#include <sys/param.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
@ -70,6 +66,7 @@
#include "transfer.h"
#include "url.h"
#include "parsedate.h" /* for the week day and month names */
#include "curlx/fopen.h"
#include "curlx/warnless.h"
#include "curl_range.h"
/* The last 3 #include files should be in this order */
@ -237,7 +234,7 @@ static CURLcode file_connect(struct Curl_easy *data, bool *done)
return CURLE_URL_MALFORMAT;
}
fd = open(actual_path, O_RDONLY|CURL_O_BINARY);
fd = curlx_open(actual_path, O_RDONLY | CURL_O_BINARY);
file->path = actual_path;
#else
if(memchr(real_path, 0, real_path_len)) {
@ -261,16 +258,16 @@ static CURLcode file_connect(struct Curl_easy *data, bool *done)
extern int __unix_path_semantics;
if(strchr(real_path + 1, ':')) {
/* Amiga absolute path */
fd = open(real_path + 1, O_RDONLY);
fd = curlx_open(real_path + 1, O_RDONLY);
file->path++;
}
else if(__unix_path_semantics) {
/* -lunix fallback */
fd = open(real_path, O_RDONLY);
fd = curlx_open(real_path, O_RDONLY);
}
}
#else
fd = open(real_path, O_RDONLY);
fd = curlx_open(real_path, O_RDONLY);
file->path = real_path;
#endif
#endif
@ -349,9 +346,9 @@ static CURLcode file_upload(struct Curl_easy *data,
#if (defined(ANDROID) || defined(__ANDROID__)) && \
(defined(__i386__) || defined(__arm__))
fd = open(file->path, mode, (mode_t)data->set.new_file_perms);
fd = curlx_open(file->path, mode, (mode_t)data->set.new_file_perms);
#else
fd = open(file->path, mode, data->set.new_file_perms);
fd = curlx_open(file->path, mode, data->set.new_file_perms);
#endif
if(fd < 0) {
failf(data, "cannot open %s for writing", file->path);

View File

@ -205,7 +205,7 @@ static FILE * vmsfopenread(const char *file, const char *mode)
struct_stat statbuf;
int result;
result = stat(file, &statbuf);
result = curlx_stat(file, &statbuf);
switch(statbuf.st_fab_rfm) {
case FAB$C_VAR:
@ -1412,7 +1412,7 @@ CURLcode curl_mime_filedata(curl_mimepart *part, const char *filename)
char *base;
struct_stat sbuf;
if(stat(filename, &sbuf))
if(curlx_stat(filename, &sbuf))
result = CURLE_READ_ERROR;
else {
part->data = strdup(filename);

View File

@ -27,15 +27,13 @@
#ifdef HAVE_NETINET_UDP_H
#include <netinet/udp.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef USE_NGHTTP3
#include <nghttp3/nghttp3.h>
#endif
#include "../urldata.h"
#include "../bufq.h"
#include "../curlx/dynbuf.h"
#include "../curlx/fopen.h"
#include "../cfilters.h"
#include "../curl_trc.h"
#include "curl_ngtcp2.h"
@ -665,8 +663,9 @@ CURLcode Curl_qlogdir(struct Curl_easy *data,
result = curlx_dyn_add(&fname, ".sqlog");
if(!result) {
int qlogfd = open(curlx_dyn_ptr(&fname), O_WRONLY|O_CREAT|CURL_O_BINARY,
data->set.new_file_perms);
int qlogfd = curlx_open(curlx_dyn_ptr(&fname),
O_WRONLY | O_CREAT | CURL_O_BINARY,
data->set.new_file_perms);
if(qlogfd != -1)
*qlogfdp = qlogfd;
}

View File

@ -30,10 +30,6 @@
#include <limits.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
@ -68,6 +64,7 @@
#include "../sockaddr.h" /* required for Curl_sockaddr_storage */
#include "../multiif.h"
#include "../select.h"
#include "../curlx/fopen.h"
#include "../curlx/warnless.h"
#include "curl_path.h"
#include "../curlx/strparse.h"
@ -1199,12 +1196,12 @@ static CURLcode ssh_state_pkey_init(struct Curl_easy *data,
sshc->rsa = aprintf("%s/.ssh/id_rsa", home);
if(!sshc->rsa)
out_of_memory = TRUE;
else if(stat(sshc->rsa, &sbuf)) {
else if(curlx_stat(sshc->rsa, &sbuf)) {
free(sshc->rsa);
sshc->rsa = aprintf("%s/.ssh/id_dsa", home);
if(!sshc->rsa)
out_of_memory = TRUE;
else if(stat(sshc->rsa, &sbuf)) {
else if(curlx_stat(sshc->rsa, &sbuf)) {
Curl_safefree(sshc->rsa);
}
}
@ -1213,10 +1210,10 @@ static CURLcode ssh_state_pkey_init(struct Curl_easy *data,
if(!out_of_memory && !sshc->rsa) {
/* Nothing found; try the current dir. */
sshc->rsa = strdup("id_rsa");
if(sshc->rsa && stat(sshc->rsa, &sbuf)) {
if(sshc->rsa && curlx_stat(sshc->rsa, &sbuf)) {
free(sshc->rsa);
sshc->rsa = strdup("id_dsa");
if(sshc->rsa && stat(sshc->rsa, &sbuf)) {
if(sshc->rsa && curlx_stat(sshc->rsa, &sbuf)) {
free(sshc->rsa);
/* Out of guesses. Set to the empty string to avoid
* surprising info messages. */

View File

@ -91,6 +91,8 @@ my %banfunc = (
"fclose" => 1,
"fdopen" => 1,
"fopen" => 1,
"open" => 1,
"stat" => 1,
);
my %warnings_extended = (

View File

@ -23,11 +23,6 @@
***************************************************************************/
#include "tool_setup.h"
#ifdef HAVE_FCNTL_H
/* for open() */
#include <fcntl.h>
#endif
#include "tool_cfgable.h"
#include "tool_msgs.h"
#include "tool_cb_wrt.h"
@ -60,7 +55,8 @@ bool tool_create_output_file(struct OutStruct *outs,
else {
int fd;
do {
fd = open(fname, O_CREAT | O_WRONLY | O_EXCL | CURL_O_BINARY, OPENMODE);
fd = curlx_open(fname, O_CREAT | O_WRONLY | O_EXCL | CURL_O_BINARY,
OPENMODE);
/* Keep retrying in the hope that it is not interrupted sometime */
/* !checksrc! disable ERRNOVAR 1 */
} while(fd == -1 && errno == EINTR);
@ -78,8 +74,9 @@ bool tool_create_output_file(struct OutStruct *outs,
return FALSE;
next_num++;
do {
fd = open(curlx_dyn_ptr(&fbuffer),
O_CREAT | O_WRONLY | O_EXCL | CURL_O_BINARY, OPENMODE);
fd = curlx_open(curlx_dyn_ptr(&fbuffer),
O_CREAT | O_WRONLY | O_EXCL | CURL_O_BINARY,
OPENMODE);
/* Keep retrying in the hope that it is not interrupted sometime */
} while(fd == -1 && errno == EINTR);
}

View File

@ -532,7 +532,7 @@ static SANITIZEcode rename_if_reserved_dos(char **const sanitized,
identify whether it is a reserved device name and not a regular
filename. */
#ifdef MSDOS
if(base && ((stat(base, &st_buf)) == 0) && (S_ISCHR(st_buf.st_mode))) {
if(base && (curlx_stat(base, &st_buf) == 0) && S_ISCHR(st_buf.st_mode)) {
/* Prepend a '_' */
size_t blen = strlen(base);
if(blen) {

View File

@ -75,7 +75,7 @@ int getfiletime(const char *filename, curl_off_t *stamp)
}
#else
struct_stat statbuf;
if(stat(filename, &statbuf) != -1) {
if(curlx_stat(filename, &statbuf) != -1) {
*stamp = (curl_off_t)statbuf.st_mtime;
rc = 0;
}

View File

@ -33,10 +33,6 @@
#endif
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include "tool_findfile.h"
#include "tool_cfgable.h"
@ -77,7 +73,7 @@ static char *checkhome(const char *home, const char *fname, bool dotscore)
else
c = aprintf("%s" DIR_CHAR "%s", home, fname);
if(c) {
int fd = open(c, O_RDONLY);
int fd = curlx_open(c, O_RDONLY);
if(fd >= 0) {
char *path = strdup(c);
close(fd);

View File

@ -30,10 +30,6 @@
#ifndef HAVE_GETPASS_R
/* this file is only for systems without getpass_r() */
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#elif defined(HAVE_TERMIO_H)
@ -178,7 +174,7 @@ char *getpass_r(const char *prompt, /* prompt to display */
{
ssize_t nread;
bool disabled;
int fd = open("/dev/tty", O_RDONLY);
int fd = curlx_open("/dev/tty", O_RDONLY);
if(fd == -1)
fd = STDIN_FILENO; /* use stdin if the tty could not be used */

View File

@ -23,10 +23,6 @@
***************************************************************************/
#include "tool_setup.h"
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif
@ -279,22 +275,22 @@ static CURLcode pre_transfer(struct per_transfer *per)
#ifdef __VMS
/* Calculate the real upload size for VMS */
per->infd = -1;
if(stat(per->uploadfile, &fileinfo) == 0) {
if(curlx_stat(per->uploadfile, &fileinfo) == 0) {
fileinfo.st_size = VmsSpecialSize(uploadfile, &fileinfo);
switch(fileinfo.st_fab_rfm) {
case FAB$C_VAR:
case FAB$C_VFC:
case FAB$C_STMCR:
per->infd = open(per->uploadfile, O_RDONLY | CURL_O_BINARY);
per->infd = curlx_open(per->uploadfile, O_RDONLY | CURL_O_BINARY);
break;
default:
per->infd = open(per->uploadfile, O_RDONLY | CURL_O_BINARY,
"rfm=stmlf", "ctx=stm");
per->infd = curlx_open(per->uploadfile, O_RDONLY | CURL_O_BINARY,
"rfm=stmlf", "ctx=stm");
}
}
if(per->infd == -1)
#else
per->infd = open(per->uploadfile, O_RDONLY | CURL_O_BINARY);
per->infd = curlx_open(per->uploadfile, O_RDONLY | CURL_O_BINARY);
if((per->infd == -1) || fstat(per->infd, &fileinfo))
#endif
{
@ -668,8 +664,7 @@ static CURLcode post_per_transfer(struct per_transfer *per,
}
if(result && config->rm_partial) {
struct_stat st;
if(!stat(outs->filename, &st) &&
S_ISREG(st.st_mode)) {
if(!curlx_stat(outs->filename, &st) && S_ISREG(st.st_mode)) {
if(!unlink(outs->filename))
notef("Removed output file: %s", outs->filename);
else
@ -974,7 +969,7 @@ static CURLcode setup_outfile(struct OperationConfig *config,
if(config->skip_existing) {
struct_stat fileinfo;
if(!stat(per->outfile, &fileinfo)) {
if(!curlx_stat(per->outfile, &fileinfo)) {
/* file is present */
notef("skips transfer, \"%s\" exists locally", per->outfile);
per->skip = TRUE;
@ -987,7 +982,7 @@ static CURLcode setup_outfile(struct OperationConfig *config,
of the file as it is now and open it for append instead */
struct_stat fileinfo;
/* VMS -- Danger, the filesize is only valid for stream files */
if(stat(per->outfile, &fileinfo) == 0)
if(curlx_stat(per->outfile, &fileinfo) == 0)
/* set offset to current file size: */
config->resume_from = fileinfo.st_size;
else

View File

@ -61,6 +61,7 @@ static CURLcode test_lib505(const char *URL)
/* get the file size of the local file */
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
hd = stat(libtest_arg2, &file_info);
#else
hd = fstat(fileno(hd_src), &file_info);

View File

@ -287,7 +287,7 @@ static int t518_test_rlimit(int keep_open)
/* open a dummy descriptor */
t518_testfd[0] = open(DEV_NULL, O_RDONLY);
t518_testfd[0] = curlx_open(DEV_NULL, O_RDONLY);
if(t518_testfd[0] < 0) {
curl_msnprintf(strbuff, sizeof(strbuff), "opening of %s failed", DEV_NULL);
t518_store_errmsg(strbuff, errno);

View File

@ -52,6 +52,7 @@ static CURLcode test_lib525(const char *URL)
/* get the file size of the local file */
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
hd = stat(libtest_arg2, &file_info);
#else
hd = fstat(fileno(hd_src), &file_info);

View File

@ -289,7 +289,7 @@ static int t537_test_rlimit(int keep_open)
/* open a dummy descriptor */
t537_testfd[0] = open(DEV_NULL, O_RDONLY);
t537_testfd[0] = curlx_open(DEV_NULL, O_RDONLY);
if(t537_testfd[0] < 0) {
curl_msnprintf(strbuff, sizeof(strbuff), "opening of %s failed", DEV_NULL);
t537_store_errmsg(strbuff, errno);

View File

@ -52,6 +52,7 @@ static CURLcode test_lib541(const char *URL)
/* get the file size of the local file */
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
hd = stat(libtest_arg2, &file_info);
#else
hd = fstat(fileno(hd_src), &file_info);

View File

@ -66,7 +66,7 @@ static CURLcode test_lib568(const char *URL)
curl_free(stream_uri);
stream_uri = NULL;
sdp = open(libtest_arg2, O_RDONLY);
sdp = curlx_open(libtest_arg2, O_RDONLY);
if(sdp == -1) {
curl_mfprintf(stderr, "can't open %s\n", libtest_arg2);
res = TEST_ERR_MAJOR_BAD;

View File

@ -84,7 +84,7 @@ static CURLcode test_lib572(const char *URL)
stream_uri = NULL;
/* PUT style GET_PARAMETERS */
params = open(libtest_arg2, O_RDONLY);
params = curlx_open(libtest_arg2, O_RDONLY);
if(params == -1) {
curl_mfprintf(stderr, "can't open %s\n", libtest_arg2);
res = TEST_ERR_MAJOR_BAD;

View File

@ -253,6 +253,7 @@ static CURLcode test_lib582(const char *URL)
/* get the file size of the local file */
#ifdef UNDER_CE
/* !checksrc! disable BANNEDFUNC 1 */
hd = stat(libtest_arg2, &file_info);
#else
hd = fstat(fileno(hd_src), &file_info);

View File

@ -3,6 +3,7 @@ allowfunc fclose
allowfunc fopen
allowfunc freeaddrinfo
allowfunc getaddrinfo
allowfunc open
allowfunc recv
allowfunc send
allowfunc socket

View File

@ -373,12 +373,12 @@ static void exit_signal_handler(int signum)
(void)!write(STDERR_FILENO, msg, sizeof(msg) - 1);
}
else {
int fd;
#ifdef _WIN32
fd = _open(serverlogfile, O_WRONLY|O_CREAT|O_APPEND, S_IREAD | S_IWRITE);
#define OPENMODE S_IREAD | S_IWRITE
#else
fd = open(serverlogfile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR | S_IWUSR);
#define OPENMODE S_IRUSR | S_IWUSR
#endif
int fd = open(serverlogfile, O_WRONLY | O_CREAT | O_APPEND, OPENMODE);
if(fd != -1) {
static const char msg[] = "exit_signal_handler: called\n";
(void)!write(fd, msg, sizeof(msg) - 1);