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
This commit is contained in:
Viktor Szakats 2025-03-16 14:19:38 +01:00
parent 131a2fd5aa
commit 04c78c897b
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
8 changed files with 37 additions and 53 deletions

View File

@ -51,7 +51,7 @@ if(CURL_BUILD_TESTING)
EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL
${HHEADERS} ${CSOURCES} ${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}) target_link_libraries(curlu PRIVATE ${CURL_LIBS})
# There is plenty of parallelism when building the testdeps target. # There is plenty of parallelism when building the testdeps target.
# Override the curlu batch size with the maximum to optimize performance. # Override the curlu batch size with the maximum to optimize performance.
@ -59,9 +59,9 @@ if(CURL_BUILD_TESTING)
endif() endif()
if(ENABLE_CURLDEBUG) if(ENABLE_CURLDEBUG)
# We must compile these sources separately to avoid memdebug.h redefinitions # We must compile this source separately to avoid memdebug.h redefinitions
# applying to them. # applying to it.
set_source_files_properties("memdebug.c" "curl_multibyte.c" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON) set_source_files_properties("memdebug.c" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
endif() endif()
## Library definition ## Library definition

View File

@ -87,9 +87,9 @@ if USE_UNITY
# in static mode. # in static mode.
curl_EXCLUDE = curl_threads.c timediff.c warnless.c curl_EXCLUDE = curl_threads.c timediff.c warnless.c
if CURLDEBUG if CURLDEBUG
# We must compile these sources separately to avoid memdebug.h redefinitions # We must compile this source separately to avoid memdebug.h redefinitions
# applying to them. # applying to it.
curl_EXCLUDE += memdebug.c curl_multibyte.c curl_EXCLUDE += memdebug.c
endif endif
# For Cygwin always compile dllmain.c as a separate unit since it # For Cygwin always compile dllmain.c as a separate unit since it
# includes windows.h, which should not be included in other units. # includes windows.h, which should not be included in other units.

View File

@ -23,11 +23,11 @@
***************************************************************************/ ***************************************************************************/
/* /*
* This file is 'mem-include-scan' clean, which means memdebug.h and * This file is 'mem-include-scan' clean, which means its memory allocations
* curl_memory.h are purposely not included in this file. See test 1132. * 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
* The functions in this file are curlx functions which are not tracked by the * these macro replacements, wrap the names in parentheses to call the original
* curl memory tracker memdebug. * versions: `ptr = (malloc)(123)`, `(free)(ptr)`, etc.
*/ */
#include "curl_setup.h" #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, int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
str_utf8, -1, NULL, 0); str_utf8, -1, NULL, 0);
if(str_w_len > 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(str_w) {
if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w, if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
str_w_len) == 0) { str_w_len) == 0) {
free(str_w); (free)(str_w);
return NULL; 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, int bytes = WideCharToMultiByte(CP_UTF8, 0, str_w, -1,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if(bytes > 0) { if(bytes > 0) {
str_utf8 = malloc(bytes); str_utf8 = (malloc)(bytes);
if(str_utf8) { if(str_utf8) {
if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, bytes, if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, bytes,
NULL, NULL) == 0) { NULL, NULL) == 0) {
free(str_utf8); (free)(str_utf8);
return NULL; 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) if(needed == (size_t)-1 || needed >= max_path_len)
goto cleanup; goto cleanup;
++needed; /* for NUL */ ++needed; /* for NUL */
ibuf = malloc(needed * sizeof(wchar_t)); ibuf = (malloc)(needed * sizeof(wchar_t));
if(!ibuf) if(!ibuf)
goto cleanup; goto cleanup;
count = mbstowcs(ibuf, in, needed); 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 */ /* skip paths that are not excessive and do not need modification */
if(needed <= MAX_PATH) if(needed <= MAX_PATH)
goto cleanup; goto cleanup;
fbuf = malloc(needed * sizeof(wchar_t)); fbuf = (malloc)(needed * sizeof(wchar_t));
if(!fbuf) if(!fbuf)
goto cleanup; goto cleanup;
count = (size_t)GetFullPathNameW(in_w, (DWORD)needed, fbuf, NULL); 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) if(needed > max_path_len)
goto cleanup; goto cleanup;
temp = malloc(needed * sizeof(wchar_t)); temp = (malloc)(needed * sizeof(wchar_t));
if(!temp) if(!temp)
goto cleanup; goto cleanup;
@ -202,7 +202,7 @@ static bool fix_excessive_path(const TCHAR *in, TCHAR **out)
if(needed > max_path_len) if(needed > max_path_len)
goto cleanup; goto cleanup;
temp = malloc(needed * sizeof(wchar_t)); temp = (malloc)(needed * sizeof(wchar_t));
if(!temp) if(!temp)
goto cleanup; goto cleanup;
@ -210,7 +210,7 @@ static bool fix_excessive_path(const TCHAR *in, TCHAR **out)
wcscpy(temp + 4, fbuf); wcscpy(temp + 4, fbuf);
} }
free(fbuf); (free)(fbuf);
fbuf = temp; 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) if(needed == (size_t)-1 || needed >= max_path_len)
goto cleanup; goto cleanup;
++needed; /* for NUL */ ++needed; /* for NUL */
obuf = malloc(needed); obuf = (malloc)(needed);
if(!obuf) if(!obuf)
goto cleanup; goto cleanup;
count = wcstombs(obuf, fbuf, needed); count = wcstombs(obuf, fbuf, needed);
@ -234,10 +234,10 @@ static bool fix_excessive_path(const TCHAR *in, TCHAR **out)
#endif #endif
cleanup: cleanup:
free(fbuf); (free)(fbuf);
#ifndef _UNICODE #ifndef _UNICODE
free(ibuf); (free)(ibuf);
free(obuf); (free)(obuf);
#endif #endif
return *out ? true : false; return *out ? true : false;
} }
@ -276,10 +276,10 @@ int curlx_win32_open(const char *filename, int oflag, ...)
target = fixed; target = fixed;
else else
target = filename; target = filename;
result = (_open)(target, oflag, pmode); result = _open(target, oflag, pmode);
#endif #endif
free(fixed); (free)(fixed);
return result; return result;
} }
@ -312,7 +312,7 @@ FILE *curlx_win32_fopen(const char *filename, const char *mode)
result = (fopen)(target, mode); result = (fopen)(target, mode);
#endif #endif
free(fixed); (free)(fixed);
return result; return result;
} }
@ -351,7 +351,7 @@ int curlx_win32_stat(const char *path, struct_stat *buffer)
#endif #endif
#endif #endif
free(fixed); (free)(fixed);
return result; return result;
} }

View File

@ -77,12 +77,6 @@ if(BUILD_STATIC_CURL)
set(CURLX_CFILES ${CURLTOOL_LIBCURL_CFILES}) set(CURLX_CFILES ${CURLTOOL_LIBCURL_CFILES})
endif() 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( add_executable(
${EXE_NAME} ${EXE_NAME}
${CURL_CFILES} ${_curl_cfiles_gen} ${CURLX_CFILES} ${CURL_HFILES} ${_curl_hfiles_gen} ${CURL_CFILES} ${_curl_cfiles_gen} ${CURLX_CFILES} ${CURL_HFILES} ${_curl_hfiles_gen}
@ -100,7 +94,7 @@ add_library(
EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL
${CURL_CFILES} ${CURLTOOL_LIBCURL_CFILES} ${CURL_HFILES} ${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}) target_link_libraries(curltool PRIVATE ${CURL_LIBS})
if(CURL_HAS_LTO) if(CURL_HAS_LTO)

View File

@ -70,22 +70,16 @@ curl_hfiles_gen =
CLEANFILES = CLEANFILES =
if USE_UNITY 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 if USE_CPPFLAG_CURL_STATICLIB
curl_CURLX = $(CURLTOOL_LIBCURL_CFILES) curl_CURLX = $(CURLTOOL_LIBCURL_CFILES)
else else
curl_CURLX = $(CURLX_CFILES) curl_CURLX = $(CURLX_CFILES)
endif endif
curltool_unity.c: $(top_srcdir)/scripts/mk-unity.pl $(CURL_CFILES) $(curl_cfiles_gen) $(curl_CURLX) 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 nodist_curl_SOURCES = curltool_unity.c
curl_SOURCES = $(curl_EXCLUDE) curl_SOURCES =
CLEANFILES += curltool_unity.c CLEANFILES += curltool_unity.c
else else
# CURL_FILES comes from Makefile.inc # CURL_FILES comes from Makefile.inc
@ -114,10 +108,10 @@ libcurltool_la_CFLAGS =
libcurltool_la_LDFLAGS = -static $(LINKFLAGS) libcurltool_la_LDFLAGS = -static $(LINKFLAGS)
if USE_UNITY if USE_UNITY
libcurltool_unity.c: $(top_srcdir)/scripts/mk-unity.pl $(CURL_CFILES) $(CURLTOOL_LIBCURL_CFILES) 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 nodist_libcurltool_la_SOURCES = libcurltool_unity.c
libcurltool_la_SOURCES = $(curl_EXCLUDE) libcurltool_la_SOURCES =
CLEANFILES += libcurltool_unity.c CLEANFILES += libcurltool_unity.c
else else
libcurltool_la_SOURCES = $(CURL_FILES) libcurltool_la_SOURCES = $(CURL_FILES)

View File

@ -27,10 +27,6 @@
curl_transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake") curl_transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
include("${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( add_custom_command(
OUTPUT "lib1521.c" OUTPUT "lib1521.c"
COMMAND ${PERL_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/mk-lib1521.pl" < "${PROJECT_SOURCE_DIR}/include/curl/curl.h" "lib1521.c" COMMAND ${PERL_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/mk-lib1521.pl" < "${PROJECT_SOURCE_DIR}/include/curl/curl.h" "lib1521.c"

View File

@ -27,7 +27,7 @@ curl_transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake") include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
if(ENABLE_SERVER_DEBUG AND ENABLE_CURLDEBUG) 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() endif()
if(CURL_TEST_BUNDLES) if(CURL_TEST_BUNDLES)

View File

@ -25,8 +25,6 @@
SERVERPROGS = resolve rtspd sockfilt sws tftpd socksd disabled mqttd SERVERPROGS = resolve rtspd sockfilt sws tftpd socksd disabled mqttd
MEMDEBUG = \ MEMDEBUG = \
../../lib/curl_multibyte.c \
../../lib/curl_multibyte.h \
../../lib/memdebug.c \ ../../lib/memdebug.c \
../../lib/memdebug.h ../../lib/memdebug.h
@ -42,6 +40,7 @@ CURLX_SRCS = \
../../lib/strcase.c \ ../../lib/strcase.c \
../../lib/strdup.c \ ../../lib/strdup.c \
../../lib/curl_get_line.c \ ../../lib/curl_get_line.c \
../../lib/curl_multibyte.c \
../../lib/version_win32.c ../../lib/version_win32.c
CURLX_HDRS = \ CURLX_HDRS = \
@ -56,6 +55,7 @@ CURLX_HDRS = \
../../lib/strcase.h \ ../../lib/strcase.h \
../../lib/strdup.h \ ../../lib/strdup.h \
../../lib/curl_get_line.h \ ../../lib/curl_get_line.h \
../../lib/curl_multibyte.h \
../../lib/version_win32.h ../../lib/version_win32.h
UTIL = \ UTIL = \