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

@ -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. */