From: Viktor Szakats Date: Mon, 16 Jun 2025 08:16:17 +0000 (+0200) Subject: windows: fixup `fopen()` in `CURLDEBUG` builds X-Git-Tag: curl-8_15_0~253 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6828009695a951195efbd6def3856feaa0e769fd;p=thirdparty%2Fcurl.git 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 cde81e4398f2944e60c73f38823dafa305a5a2f4, 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 cde81e4398f2944e60c73f38823dafa305a5a2f4 #17631 Closes #16747 --- diff --git a/lib/curl_memory.h b/lib/curl_memory.h index 00a26b6980..0faf928617 100644 --- a/lib/curl_memory.h +++ b/lib/curl_memory.h @@ -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 diff --git a/lib/curl_setup.h b/lib/curl_setup.h index ae89cc8e44..79a658fda5 100644 --- a/lib/curl_setup.h +++ b/lib/curl_setup.h @@ -470,62 +470,46 @@ #include #endif -/* - * Large file (>2Gb) support using Win32 functions. - */ - -#ifdef USE_WIN32_LARGE_FILES +#ifdef _WIN32 # ifdef HAVE_IO_H # include # endif # include # include -# 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 */ -# include -# 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 +# 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 -# include -# include # 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); +# 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 -# define LSEEK_ERROR (long)-1 +#elif defined(__DJGPP__) + /* Requires DJGPP 2.04 */ +# include +# undef lseek +# define lseek(fdes,offset,whence) llseek(fdes, offset, whence) +# define LSEEK_ERROR (offset_t)-1 #endif #ifndef struct_stat diff --git a/lib/memdebug.c b/lib/memdebug.c index 832944d2b0..5bce90bd93 100644 --- a/lib/memdebug.c +++ b/lib/memdebug.c @@ -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",