windows: fixup fopen() in CURLDEBUG builds

Introduce an immutable `CURL_FOPEN()` macro to store the `fopen()`
mapping on Windows. Then use that instead `(fopen)` from `memdebug.c`.
It makes CURLDEBUG builds use the correct `fopen` wrapper on Windows.
This macro is only defined on Windows, as of this patch.

This is necessary after cde81e4398,
which no longer applies the default `fopen()` override to `memdebug.c`.

Also:
- curl_setup.h: de-dupe, simplify Windows file I/O function overrides.
- curl_memory.h: fix to reset `fopen` to `curlx_win32_fopen()` on
  Windows. Before this patch it reset it to stock `fopen()`.

Follow-up to cde81e4398 #17631

Closes #16747
This commit is contained in:
Viktor Szakats 2025-06-16 10:16:17 +02:00
parent cde81e4398
commit 6828009695
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
3 changed files with 44 additions and 48 deletions

View File

@ -87,6 +87,9 @@
#undef sclose
#define sclose(x) CURL_SCLOSE(x)
#undef fopen
#ifdef CURL_FOPEN
#define fopen(fname, mode) CURL_FOPEN(fname, mode)
#endif
#undef fdopen
#undef fclose

View File

@ -470,64 +470,48 @@
#include <curl/stdcheaders.h>
#endif
/*
* Large file (>2Gb) support using Win32 functions.
*/
#ifdef USE_WIN32_LARGE_FILES
#ifdef _WIN32
# ifdef HAVE_IO_H
# include <io.h>
# endif
# include <sys/types.h>
# include <sys/stat.h>
# undef lseek
# define lseek(fdes,offset,whence) _lseeki64(fdes, offset, whence)
# undef fstat
# define fstat(fdes,stp) _fstati64(fdes, stp)
# undef stat
# define stat(fname,stp) curlx_win32_stat(fname, stp)
# define struct_stat struct _stati64
# define LSEEK_ERROR (__int64)-1
# define open curlx_win32_open
# define fopen(fname,mode) curlx_win32_fopen(fname, mode)
int curlx_win32_open(const char *filename, int oflag, ...);
int curlx_win32_stat(const char *path, struct_stat *buffer);
FILE *curlx_win32_fopen(const char *filename, const char *mode);
#endif
#ifdef __DJGPP__
/* Requires DJGPP 2.04 */
# ifdef USE_WIN32_LARGE_FILES
/* Large file (>2Gb) support using Win32 functions. */
# undef lseek
# define lseek(fdes, offset, whence) _lseeki64(fdes, offset, whence)
# undef fstat
# define fstat(fdes,stp) _fstati64(fdes, stp)
# undef stat
# define struct_stat struct _stati64
# define LSEEK_ERROR (__int64)-1
# else
/* Small file (<2Gb) support using Win32 functions. */
# ifndef UNDER_CE
# undef lseek
# define lseek(fdes, offset, whence) _lseek(fdes, (long)offset, whence)
# define fstat(fdes, stp) _fstat(fdes, stp)
# define struct_stat struct _stat
# 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, ...);
FILE *curlx_win32_fopen(const char *filename, const char *mode);
# define stat(fname, stp) curlx_win32_stat(fname, stp)
# define open curlx_win32_open
# define CURL_FOPEN(fname, mode) curlx_win32_fopen(fname, mode)
# define fopen(fname, mode) CURL_FOPEN(fname, mode)
# endif
#elif defined(__DJGPP__)
/* Requires DJGPP 2.04 */
# include <unistd.h>
# undef lseek
# define lseek(fdes,offset,whence) llseek(fdes, offset, whence)
# define LSEEK_ERROR (offset_t)-1
#endif
/*
* Small file (<2Gb) support using Win32 functions.
*/
#if defined(_WIN32) && !defined(USE_WIN32_LARGE_FILES)
# ifdef HAVE_IO_H
# include <io.h>
# endif
# include <sys/types.h>
# include <sys/stat.h>
# ifndef UNDER_CE
# undef lseek
# define lseek(fdes,offset,whence) _lseek(fdes, (long)offset, whence)
# define fstat(fdes,stp) _fstat(fdes, stp)
# define stat(fname,stp) curlx_win32_stat(fname, stp)
# define struct_stat struct _stat
# define open curlx_win32_open
# define fopen(fname,mode) curlx_win32_fopen(fname, mode)
int curlx_win32_stat(const char *path, struct_stat *buffer);
int curlx_win32_open(const char *filename, int oflag, ...);
FILE *curlx_win32_fopen(const char *filename, const char *mode);
# endif
# define LSEEK_ERROR (long)-1
#endif
#ifndef struct_stat
#define struct_stat struct stat
#endif

View File

@ -78,7 +78,11 @@ void curl_dbg_memdebug(const char *logname)
{
if(!curl_dbg_logfile) {
if(logname && *logname)
#ifdef CURL_FOPEN
curl_dbg_logfile = CURL_FOPEN(logname, FOPEN_WRITETEXT);
#else
curl_dbg_logfile = (fopen)(logname, FOPEN_WRITETEXT);
#endif
else
curl_dbg_logfile = stderr;
#ifdef MEMDEBUG_LOG_SYNC
@ -414,7 +418,12 @@ ALLOC_FUNC
FILE *curl_dbg_fopen(const char *file, const char *mode,
int line, const char *source)
{
FILE *res = (fopen)(file, mode);
FILE *res;
#ifdef CURL_FOPEN
res = CURL_FOPEN(file, mode);
#else
res = (fopen)(file, mode);
#endif
if(source)
curl_dbg_log("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",