From 04c78c897b6ad10bad526b72f66570dad37a41d0 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Sun, 16 Mar 2025 14:19:38 +0100 Subject: [PATCH] curl_multibyte: fixup low-level calls, include in unity builds Also adjust `()` around low-level calls preventing macro overrides via e.g. `memdebug.h`: - add for `malloc` and `free`. - drop for `_open`. (We do not override `_open` in curl.) Tidy-up: also sync libcurlu custom macro order in cmake with autotools. Follow-up to f42a279ee32d3db3ab529da8dfb833edb5a088ca #11928 Closes #16742 --- lib/CMakeLists.txt | 8 +++---- lib/Makefile.am | 6 ++--- lib/curl_multibyte.c | 44 ++++++++++++++++++------------------ src/CMakeLists.txt | 8 +------ src/Makefile.am | 14 ++++-------- tests/libtest/CMakeLists.txt | 4 ---- tests/server/CMakeLists.txt | 2 +- tests/server/Makefile.inc | 4 ++-- 8 files changed, 37 insertions(+), 53 deletions(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 6fd362acae..47641271f2 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -51,7 +51,7 @@ if(CURL_BUILD_TESTING) EXCLUDE_FROM_ALL ${HHEADERS} ${CSOURCES} ) - target_compile_definitions(curlu PUBLIC "UNITTESTS" "CURL_STATICLIB") + target_compile_definitions(curlu PUBLIC "CURL_STATICLIB" "UNITTESTS") target_link_libraries(curlu PRIVATE ${CURL_LIBS}) # There is plenty of parallelism when building the testdeps target. # Override the curlu batch size with the maximum to optimize performance. @@ -59,9 +59,9 @@ if(CURL_BUILD_TESTING) endif() if(ENABLE_CURLDEBUG) - # We must compile these sources separately to avoid memdebug.h redefinitions - # applying to them. - set_source_files_properties("memdebug.c" "curl_multibyte.c" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON) + # We must compile this source separately to avoid memdebug.h redefinitions + # applying to it. + set_source_files_properties("memdebug.c" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON) endif() ## Library definition diff --git a/lib/Makefile.am b/lib/Makefile.am index b10c9a13be..c810129ed7 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -87,9 +87,9 @@ if USE_UNITY # in static mode. curl_EXCLUDE = curl_threads.c timediff.c warnless.c if CURLDEBUG -# We must compile these sources separately to avoid memdebug.h redefinitions -# applying to them. -curl_EXCLUDE += memdebug.c curl_multibyte.c +# We must compile this source separately to avoid memdebug.h redefinitions +# applying to it. +curl_EXCLUDE += memdebug.c endif # For Cygwin always compile dllmain.c as a separate unit since it # includes windows.h, which should not be included in other units. diff --git a/lib/curl_multibyte.c b/lib/curl_multibyte.c index 3c8077d9de..e8e9ec1f81 100644 --- a/lib/curl_multibyte.c +++ b/lib/curl_multibyte.c @@ -23,11 +23,11 @@ ***************************************************************************/ /* - * This file is 'mem-include-scan' clean, which means memdebug.h and - * curl_memory.h are purposely not included in this file. See test 1132. - * - * The functions in this file are curlx functions which are not tracked by the - * curl memory tracker memdebug. + * This file is 'mem-include-scan' clean, which means its memory allocations + * are not tracked by the curl memory tracker memdebug, so they must not use + * `CURLDEBUG` macro replacements in memdebug.h for free, malloc, etc. To avoid + * these macro replacements, wrap the names in parentheses to call the original + * versions: `ptr = (malloc)(123)`, `(free)(ptr)`, etc. */ #include "curl_setup.h" @@ -48,11 +48,11 @@ wchar_t *curlx_convert_UTF8_to_wchar(const char *str_utf8) int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str_utf8, -1, NULL, 0); if(str_w_len > 0) { - str_w = malloc(str_w_len * sizeof(wchar_t)); + str_w = (malloc)(str_w_len * sizeof(wchar_t)); if(str_w) { if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w, str_w_len) == 0) { - free(str_w); + (free)(str_w); return NULL; } } @@ -70,11 +70,11 @@ char *curlx_convert_wchar_to_UTF8(const wchar_t *str_w) int bytes = WideCharToMultiByte(CP_UTF8, 0, str_w, -1, NULL, 0, NULL, NULL); if(bytes > 0) { - str_utf8 = malloc(bytes); + str_utf8 = (malloc)(bytes); if(str_utf8) { if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, bytes, NULL, NULL) == 0) { - free(str_utf8); + (free)(str_utf8); return NULL; } } @@ -136,7 +136,7 @@ static bool fix_excessive_path(const TCHAR *in, TCHAR **out) if(needed == (size_t)-1 || needed >= max_path_len) goto cleanup; ++needed; /* for NUL */ - ibuf = malloc(needed * sizeof(wchar_t)); + ibuf = (malloc)(needed * sizeof(wchar_t)); if(!ibuf) goto cleanup; count = mbstowcs(ibuf, in, needed); @@ -156,7 +156,7 @@ static bool fix_excessive_path(const TCHAR *in, TCHAR **out) /* skip paths that are not excessive and do not need modification */ if(needed <= MAX_PATH) goto cleanup; - fbuf = malloc(needed * sizeof(wchar_t)); + fbuf = (malloc)(needed * sizeof(wchar_t)); if(!fbuf) goto cleanup; count = (size_t)GetFullPathNameW(in_w, (DWORD)needed, fbuf, NULL); @@ -189,7 +189,7 @@ static bool fix_excessive_path(const TCHAR *in, TCHAR **out) if(needed > max_path_len) goto cleanup; - temp = malloc(needed * sizeof(wchar_t)); + temp = (malloc)(needed * sizeof(wchar_t)); if(!temp) goto cleanup; @@ -202,7 +202,7 @@ static bool fix_excessive_path(const TCHAR *in, TCHAR **out) if(needed > max_path_len) goto cleanup; - temp = malloc(needed * sizeof(wchar_t)); + temp = (malloc)(needed * sizeof(wchar_t)); if(!temp) goto cleanup; @@ -210,7 +210,7 @@ static bool fix_excessive_path(const TCHAR *in, TCHAR **out) wcscpy(temp + 4, fbuf); } - free(fbuf); + (free)(fbuf); fbuf = temp; } @@ -220,7 +220,7 @@ static bool fix_excessive_path(const TCHAR *in, TCHAR **out) if(needed == (size_t)-1 || needed >= max_path_len) goto cleanup; ++needed; /* for NUL */ - obuf = malloc(needed); + obuf = (malloc)(needed); if(!obuf) goto cleanup; count = wcstombs(obuf, fbuf, needed); @@ -234,10 +234,10 @@ static bool fix_excessive_path(const TCHAR *in, TCHAR **out) #endif cleanup: - free(fbuf); + (free)(fbuf); #ifndef _UNICODE - free(ibuf); - free(obuf); + (free)(ibuf); + (free)(obuf); #endif return *out ? true : false; } @@ -276,10 +276,10 @@ int curlx_win32_open(const char *filename, int oflag, ...) target = fixed; else target = filename; - result = (_open)(target, oflag, pmode); + result = _open(target, oflag, pmode); #endif - free(fixed); + (free)(fixed); return result; } @@ -312,7 +312,7 @@ FILE *curlx_win32_fopen(const char *filename, const char *mode) result = (fopen)(target, mode); #endif - free(fixed); + (free)(fixed); return result; } @@ -351,7 +351,7 @@ int curlx_win32_stat(const char *path, struct_stat *buffer) #endif #endif - free(fixed); + (free)(fixed); return result; } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 289c99c0cf..a0b700bce4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -77,12 +77,6 @@ if(BUILD_STATIC_CURL) set(CURLX_CFILES ${CURLTOOL_LIBCURL_CFILES}) endif() -if(ENABLE_CURLDEBUG) - # We must compile this source separately to avoid memdebug.h redefinitions - # applying to them. - set_source_files_properties("../lib/curl_multibyte.c" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON) -endif() - add_executable( ${EXE_NAME} ${CURL_CFILES} ${_curl_cfiles_gen} ${CURLX_CFILES} ${CURL_HFILES} ${_curl_hfiles_gen} @@ -100,7 +94,7 @@ add_library( EXCLUDE_FROM_ALL ${CURL_CFILES} ${CURLTOOL_LIBCURL_CFILES} ${CURL_HFILES} ) -target_compile_definitions(curltool PUBLIC "UNITTESTS" "CURL_STATICLIB") +target_compile_definitions(curltool PUBLIC "CURL_STATICLIB" "UNITTESTS") target_link_libraries(curltool PRIVATE ${CURL_LIBS}) if(CURL_HAS_LTO) diff --git a/src/Makefile.am b/src/Makefile.am index dcf1e371c8..dc431e1395 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -70,22 +70,16 @@ curl_hfiles_gen = CLEANFILES = if USE_UNITY -curl_EXCLUDE = -if CURLDEBUG -# We must compile this source separately to avoid memdebug.h redefinitions -# applying to them. -curl_EXCLUDE += ../lib/curl_multibyte.c -endif if USE_CPPFLAG_CURL_STATICLIB curl_CURLX = $(CURLTOOL_LIBCURL_CFILES) else curl_CURLX = $(CURLX_CFILES) endif curltool_unity.c: $(top_srcdir)/scripts/mk-unity.pl $(CURL_CFILES) $(curl_cfiles_gen) $(curl_CURLX) - @PERL@ $(top_srcdir)/scripts/mk-unity.pl $(srcdir) $(CURL_CFILES) $(curl_cfiles_gen) $(curl_CURLX) --exclude $(curl_EXCLUDE) > curltool_unity.c + @PERL@ $(top_srcdir)/scripts/mk-unity.pl $(srcdir) $(CURL_CFILES) $(curl_cfiles_gen) $(curl_CURLX) > curltool_unity.c nodist_curl_SOURCES = curltool_unity.c -curl_SOURCES = $(curl_EXCLUDE) +curl_SOURCES = CLEANFILES += curltool_unity.c else # CURL_FILES comes from Makefile.inc @@ -114,10 +108,10 @@ libcurltool_la_CFLAGS = libcurltool_la_LDFLAGS = -static $(LINKFLAGS) if USE_UNITY libcurltool_unity.c: $(top_srcdir)/scripts/mk-unity.pl $(CURL_CFILES) $(CURLTOOL_LIBCURL_CFILES) - @PERL@ $(top_srcdir)/scripts/mk-unity.pl $(srcdir) $(CURL_CFILES) $(CURLTOOL_LIBCURL_CFILES) --exclude $(curl_EXCLUDE) > libcurltool_unity.c + @PERL@ $(top_srcdir)/scripts/mk-unity.pl $(srcdir) $(CURL_CFILES) $(CURLTOOL_LIBCURL_CFILES) > libcurltool_unity.c nodist_libcurltool_la_SOURCES = libcurltool_unity.c -libcurltool_la_SOURCES = $(curl_EXCLUDE) +libcurltool_la_SOURCES = CLEANFILES += libcurltool_unity.c else libcurltool_la_SOURCES = $(CURL_FILES) diff --git a/tests/libtest/CMakeLists.txt b/tests/libtest/CMakeLists.txt index 9a50afe8ac..f6f80d6df9 100644 --- a/tests/libtest/CMakeLists.txt +++ b/tests/libtest/CMakeLists.txt @@ -27,10 +27,6 @@ curl_transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake") include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake") -if(ENABLE_CURLDEBUG) - set_source_files_properties("../../lib/curl_multibyte.c" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON) -endif() - add_custom_command( OUTPUT "lib1521.c" COMMAND ${PERL_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/mk-lib1521.pl" < "${PROJECT_SOURCE_DIR}/include/curl/curl.h" "lib1521.c" diff --git a/tests/server/CMakeLists.txt b/tests/server/CMakeLists.txt index 1a60a931a5..840d6ece8f 100644 --- a/tests/server/CMakeLists.txt +++ b/tests/server/CMakeLists.txt @@ -27,7 +27,7 @@ curl_transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake") if(ENABLE_SERVER_DEBUG AND ENABLE_CURLDEBUG) - set_source_files_properties("../../lib/memdebug.c" "../../lib/curl_multibyte.c" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON) + set_source_files_properties("../../lib/memdebug.c" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON) endif() if(CURL_TEST_BUNDLES) diff --git a/tests/server/Makefile.inc b/tests/server/Makefile.inc index f3062c8d34..5633b04676 100644 --- a/tests/server/Makefile.inc +++ b/tests/server/Makefile.inc @@ -25,8 +25,6 @@ SERVERPROGS = resolve rtspd sockfilt sws tftpd socksd disabled mqttd MEMDEBUG = \ - ../../lib/curl_multibyte.c \ - ../../lib/curl_multibyte.h \ ../../lib/memdebug.c \ ../../lib/memdebug.h @@ -42,6 +40,7 @@ CURLX_SRCS = \ ../../lib/strcase.c \ ../../lib/strdup.c \ ../../lib/curl_get_line.c \ + ../../lib/curl_multibyte.c \ ../../lib/version_win32.c CURLX_HDRS = \ @@ -56,6 +55,7 @@ CURLX_HDRS = \ ../../lib/strcase.h \ ../../lib/strdup.h \ ../../lib/curl_get_line.h \ + ../../lib/curl_multibyte.h \ ../../lib/version_win32.h UTIL = \