]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
curl_multibyte: fixup low-level calls, include in unity builds
authorViktor Szakats <commit@vsz.me>
Sun, 16 Mar 2025 13:19:38 +0000 (14:19 +0100)
committerViktor Szakats <commit@vsz.me>
Mon, 7 Apr 2025 20:33:24 +0000 (22:33 +0200)
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
lib/Makefile.am
lib/curl_multibyte.c
src/CMakeLists.txt
src/Makefile.am
tests/libtest/CMakeLists.txt
tests/server/CMakeLists.txt
tests/server/Makefile.inc

index 6fd362acaeb413c17f674d7dbd588443ae4e330e..47641271f2b3f051cb5dfebd11ba8b4e53b62a52 100644 (file)
@@ -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
index b10c9a13beb3ffb6fc716ac54993d27ba9df9cec..c810129ed76093c56076e3cb0ef38d7f7b45b3e0 100644 (file)
@@ -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.
index 3c8077d9de3a5920133184e3396c0a4ef97c1fc8..e8e9ec1f81395d93be45a3358fff3b791973a728 100644 (file)
  ***************************************************************************/
 
 /*
- * 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;
 }
 
index 289c99c0cfd168b083f6364ceccf0e1e808845c1..a0b700bce4160894a380d07ec8ea7ef053f6bc40 100644 (file)
@@ -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)
index dcf1e371c8760409a750027adb9147e5bb9eb317..dc431e13950b3a910e2528b1f22565722ef2540d 100644 (file)
@@ -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)
index 9a50afe8acab01462087a8513d6a7955a11a3fcf..f6f80d6df984321cd3e48fa7eb93fea111bab6d8 100644 (file)
 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"
index 1a60a931a54c17404e90b88ba3a3e217889017cf..840d6ece8fe83d0c86bb40939257ce0dc9802206 100644 (file)
@@ -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)
index f3062c8d34670c36888e067024ad4d431f530899..5633b04676aaba211ab4dca7a15847c242a29a15 100644 (file)
@@ -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 = \