curlx: use curlx allocators in non-memdebug builds (Windows)
To limit raw allocators to `CURLDEBUG` (memdebug/TrackMemory) Windows
UNICODE builds.
Closes #19788
diff --git a/lib/curlx/fopen.c b/lib/curlx/fopen.c
index caf4978..1838b7d 100644
--- a/lib/curlx/fopen.c
+++ b/lib/curlx/fopen.c
@@ -100,8 +100,7 @@
goto cleanup;
if(!needed || needed >= max_path_len)
goto cleanup;
- /* !checksrc! disable BANNEDFUNC 1 */
- ibuf = malloc(needed * sizeof(wchar_t));
+ ibuf = CURLX_MALLOC(needed * sizeof(wchar_t));
if(!ibuf)
goto cleanup;
if(mbstowcs_s(&count, ibuf, needed, in, needed - 1))
@@ -122,8 +121,7 @@
/* skip paths that are not excessive and do not need modification */
if(needed <= MAX_PATH)
goto cleanup;
- /* !checksrc! disable BANNEDFUNC 1 */
- fbuf = malloc(needed * sizeof(wchar_t));
+ fbuf = CURLX_MALLOC(needed * sizeof(wchar_t));
if(!fbuf)
goto cleanup;
count = (size_t)GetFullPathNameW(in_w, (DWORD)needed, fbuf, NULL);
@@ -156,19 +154,16 @@
if(needed > max_path_len)
goto cleanup;
- /* !checksrc! disable BANNEDFUNC 1 */
- temp = malloc(needed * sizeof(wchar_t));
+ temp = CURLX_MALLOC(needed * sizeof(wchar_t));
if(!temp)
goto cleanup;
if(wcsncpy_s(temp, needed, L"\\\\?\\UNC\\", 8)) {
- /* !checksrc! disable BANNEDFUNC 1 */
- free(temp);
+ CURLX_FREE(temp);
goto cleanup;
}
if(wcscpy_s(temp + 8, needed, fbuf + 2)) {
- /* !checksrc! disable BANNEDFUNC 1 */
- free(temp);
+ CURLX_FREE(temp);
goto cleanup;
}
}
@@ -178,25 +173,21 @@
if(needed > max_path_len)
goto cleanup;
- /* !checksrc! disable BANNEDFUNC 1 */
- temp = malloc(needed * sizeof(wchar_t));
+ temp = CURLX_MALLOC(needed * sizeof(wchar_t));
if(!temp)
goto cleanup;
if(wcsncpy_s(temp, needed, L"\\\\?\\", 4)) {
- /* !checksrc! disable BANNEDFUNC 1 */
- free(temp);
+ CURLX_FREE(temp);
goto cleanup;
}
if(wcscpy_s(temp + 4, needed, fbuf)) {
- /* !checksrc! disable BANNEDFUNC 1 */
- free(temp);
+ CURLX_FREE(temp);
goto cleanup;
}
}
- /* !checksrc! disable BANNEDFUNC 1 */
- free(fbuf);
+ CURLX_FREE(fbuf);
fbuf = temp;
}
@@ -206,8 +197,7 @@
goto cleanup;
if(!needed || needed >= max_path_len)
goto cleanup;
- /* !checksrc! disable BANNEDFUNC 1 */
- obuf = malloc(needed);
+ obuf = CURLX_MALLOC(needed);
if(!obuf)
goto cleanup;
if(wcstombs_s(&count, obuf, needed, fbuf, needed - 1))
@@ -222,12 +212,10 @@
#endif
cleanup:
- /* !checksrc! disable BANNEDFUNC 1 */
- free(fbuf);
+ CURLX_FREE(fbuf);
#ifndef _UNICODE
- /* !checksrc! disable BANNEDFUNC 2 */
- free(ibuf);
- free(obuf);
+ CURLX_FREE(ibuf);
+ CURLX_FREE(obuf);
#endif
return *out ? true : false;
}
@@ -269,8 +257,7 @@
errno = _sopen_s(&result, target, oflag, _SH_DENYNO, pmode);
#endif
- /* !checksrc! disable BANNEDFUNC 1 */
- free(fixed);
+ CURLX_FREE(fixed);
return result;
}
@@ -303,8 +290,7 @@
errno = fopen_s(&result, target, mode);
#endif
- /* !checksrc! disable BANNEDFUNC 1 */
- free(fixed);
+ CURLX_FREE(fixed);
return result;
}
@@ -342,8 +328,7 @@
errno = freopen_s(&result, target, mode, fp);
#endif
- /* !checksrc! disable BANNEDFUNC 1 */
- free(fixed);
+ CURLX_FREE(fixed);
return result;
}
@@ -382,8 +367,7 @@
#endif
#endif
- /* !checksrc! disable BANNEDFUNC 1 */
- free(fixed);
+ CURLX_FREE(fixed);
return result;
}
diff --git a/lib/curlx/multibyte.c b/lib/curlx/multibyte.c
index 2583f30..fb3b7be 100644
--- a/lib/curlx/multibyte.c
+++ b/lib/curlx/multibyte.c
@@ -45,13 +45,11 @@
int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
str_utf8, -1, NULL, 0);
if(str_w_len > 0) {
- /* !checksrc! disable BANNEDFUNC 1 */
- str_w = malloc(str_w_len * sizeof(wchar_t));
+ str_w = CURLX_MALLOC(str_w_len * sizeof(wchar_t));
if(str_w) {
if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
str_w_len) == 0) {
- /* !checksrc! disable BANNEDFUNC 1 */
- free(str_w);
+ CURLX_FREE(str_w);
return NULL;
}
}
@@ -69,13 +67,11 @@
int bytes = WideCharToMultiByte(CP_UTF8, 0, str_w, -1,
NULL, 0, NULL, NULL);
if(bytes > 0) {
- /* !checksrc! disable BANNEDFUNC 1 */
- str_utf8 = malloc(bytes);
+ str_utf8 = CURLX_MALLOC(bytes);
if(str_utf8) {
if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, bytes,
NULL, NULL) == 0) {
- /* !checksrc! disable BANNEDFUNC 1 */
- free(str_utf8);
+ CURLX_FREE(str_utf8);
return NULL;
}
}
diff --git a/lib/curlx/multibyte.h b/lib/curlx/multibyte.h
index fac07c2..3eae4e7 100644
--- a/lib/curlx/multibyte.h
+++ b/lib/curlx/multibyte.h
@@ -44,12 +44,20 @@
* memory tracker memdebug functions.
*/
+#ifdef CURLDEBUG
+#define CURLX_MALLOC(x) malloc(x)
+#define CURLX_FREE(x) free(x)
+#else
+#define CURLX_MALLOC(x) curlx_malloc(x)
+#define CURLX_FREE(x) curlx_free(x)
+#endif
+
/* MultiByte conversions using Windows kernel32 library. */
wchar_t *curlx_convert_UTF8_to_wchar(const char *str_utf8);
char *curlx_convert_wchar_to_UTF8(const wchar_t *str_w);
/* the purpose of this macro is to free() without being traced by memdebug */
-#define curlx_unicodefree(ptr) free(ptr)
+#define curlx_unicodefree(ptr) CURLX_FREE(ptr)
#ifdef UNICODE
@@ -65,8 +73,13 @@
#else /* !UNICODE */
+#ifdef CURLDEBUG
#define curlx_convert_UTF8_to_tchar(ptr) _strdup(ptr)
#define curlx_convert_tchar_to_UTF8(ptr) _strdup(ptr)
+#else
+#define curlx_convert_UTF8_to_tchar(ptr) curlx_strdup(ptr)
+#define curlx_convert_tchar_to_UTF8(ptr) curlx_strdup(ptr)
+#endif
typedef union {
char *tchar_ptr;