build: stop overriding standard memory allocation functions

Before this patch curl used the C preprocessor to override standard
memory allocation symbols: malloc, calloc, strdup, realloc, free.
The goal of these is to replace them with curl's debug wrappers in
`CURLDEBUG` builds, another was to replace them with the wrappers
calling user-defined allocators in libcurl. This solution needed a bunch
of workarounds to avoid breaking external headers: it relied on include
order to do the overriding last. For "unity" builds it needed to reset
overrides before external includes. Also in test apps, which are always
built as single source files. It also needed the `(symbol)` trick
to avoid overrides in some places. This would still not fix cases where
the standard symbols were macros. It was also fragile and difficult
to figure out which was the actual function behind an alloc or free call
in a specific piece of code. This in turn caused bugs where the wrong
allocator was accidentally called.

To avoid these problems, this patch replaces this solution with
`curlx_`-prefixed allocator macros, and mapping them _once_ to either
the libcurl wrappers, the debug wrappers or the standard ones, matching
the rest of the code in libtests.

This concludes the long journey to avoid redefining standard functions
in the curl codebase.

Note: I did not update `packages/OS400/*.c` sources. They did not
`#include` `curl_setup.h`, `curl_memory.h` or `memdebug.h`, meaning
the overrides were never applied to them. This may or may not have been
correct. For now I suppressed the direct use of standard allocators
via a local `.checksrc`. Probably they (except for `curlcl.c`) should be
updated to include `curl_setup.h` and use the `curlx_` macros.

This patch changes mappings in two places:
- `lib/curl_threads.c` in libtests: Before this patch it mapped to
  libcurl allocators. After, it maps to standard allocators, like
  the rest of libtests code.
- `units`: before this patch it mapped to standard allocators. After, it
  maps to libcurl allocators.

Also:
- drop all position-dependent `curl_memory.h` and `memdebug.h` includes,
  and delete the now unnecessary headers.
- rename `Curl_tcsdup` macro to `curlx_tcsdup` and define like the other
  allocators.
- map `curlx_strdup()` to `_strdup()` on Windows (was: `strdup()`).
  To fix warnings silenced via `_CRT_NONSTDC_NO_DEPRECATE`.
- multibyte: map `curlx_convert_*()` to `_strdup()` on Windows
  (was: `strdup()`).
- src: do not reuse the `strdup` name for the local replacement.
- lib509: call `_strdup()` on Windows (was: `strdup()`).
- test1132: delete test obsoleted by this patch.
- CHECKSRC.md: update text for `SNPRINTF`.
- checksrc: ban standard allocator symbols.

Follow-up to b12da22db1 #18866
Follow-up to db98daab05 #18844
Follow-up to 4deea9396b #18814
Follow-up to 9678ff5b1b #18776
Follow-up to 10bac43b87 #18774
Follow-up to 20142f5d06 #18634
Follow-up to bf7375ecc5 #18503
Follow-up to 9863599d69 #18502
Follow-up to 3bb5e58c10 #17827

Closes #19626
This commit is contained in:
Viktor Szakats 2025-10-08 02:33:19 +02:00
parent bfc3d131b6
commit 193cb00ce9
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
471 changed files with 1456 additions and 2785 deletions

View file

@ -37,10 +37,6 @@
#include "curlx/strparse.h"
#include "curl_memrchr.h"
/* The last 2 #include files should be in this order */
#include "curl_memory.h"
#include "memdebug.h"
#ifdef _WIN32
/* MS-DOS/Windows style drive prefix, eg c: in c:foo */
#define STARTS_WITH_DRIVE_PREFIX(str) \
@ -94,16 +90,16 @@ static CURLUcode parseurl_and_replace(const char *url, CURLU *u,
static void free_urlhandle(struct Curl_URL *u)
{
free(u->scheme);
free(u->user);
free(u->password);
free(u->options);
free(u->host);
free(u->zoneid);
free(u->port);
free(u->path);
free(u->query);
free(u->fragment);
curlx_free(u->scheme);
curlx_free(u->user);
curlx_free(u->password);
curlx_free(u->options);
curlx_free(u->host);
curlx_free(u->zoneid);
curlx_free(u->port);
curlx_free(u->path);
curlx_free(u->query);
curlx_free(u->fragment);
}
/*
@ -384,17 +380,17 @@ static CURLUcode parse_hostname_login(struct Curl_URL *u,
result = CURLUE_USER_NOT_ALLOWED;
goto out;
}
free(u->user);
curlx_free(u->user);
u->user = userp;
}
if(passwdp) {
free(u->password);
curlx_free(u->password);
u->password = passwdp;
}
if(optionsp) {
free(u->options);
curlx_free(u->options);
u->options = optionsp;
}
@ -404,9 +400,9 @@ static CURLUcode parse_hostname_login(struct Curl_URL *u,
out:
free(userp);
free(passwdp);
free(optionsp);
curlx_free(userp);
curlx_free(passwdp);
curlx_free(optionsp);
u->user = NULL;
u->password = NULL;
u->options = NULL;
@ -459,7 +455,7 @@ UNITTEST CURLUcode Curl_parse_port(struct Curl_URL *u, struct dynbuf *host,
u->portnum = (unsigned short) port;
/* generate a new port number string to get rid of leading zeroes etc */
free(u->port);
curlx_free(u->port);
u->port = curl_maprintf("%" CURL_FORMAT_CURL_OFF_T, port);
if(!u->port)
return CURLUE_OUT_OF_MEMORY;
@ -497,7 +493,7 @@ static CURLUcode ipv6_parse(struct Curl_URL *u, char *hostname,
if(!i || (']' != *h))
return CURLUE_BAD_IPV6;
zoneid[i] = 0;
u->zoneid = strdup(zoneid);
u->zoneid = curlx_strdup(zoneid);
if(!u->zoneid)
return CURLUE_OUT_OF_MEMORY;
hostname[len] = ']'; /* insert end bracket */
@ -675,7 +671,7 @@ static CURLUcode urldecode_host(struct dynbuf *host)
return CURLUE_BAD_HOSTNAME;
curlx_dyn_reset(host);
result = curlx_dyn_addn(host, decoded, dlen);
free(decoded);
curlx_free(decoded);
if(result)
return cc2cu(result);
}
@ -750,7 +746,7 @@ CURLUcode Curl_url_set_authority(CURLU *u, const char *authority)
if(result)
curlx_dyn_free(&host);
else {
free(u->host);
curlx_free(u->host);
u->host = curlx_dyn_ptr(&host);
}
return result;
@ -894,7 +890,7 @@ end:
if(curlx_dyn_len(&out))
*outp = curlx_dyn_ptr(&out);
else {
*outp = strdup("");
*outp = curlx_strdup("");
if(!*outp)
return 1;
}
@ -940,7 +936,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
path = &url[5];
pathlen = urllen - 5;
u->scheme = strdup("file");
u->scheme = curlx_strdup("file");
if(!u->scheme) {
result = CURLUE_OUT_OF_MEMORY;
goto fail;
@ -1087,7 +1083,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
}
if(schemep) {
u->scheme = strdup(schemep);
u->scheme = curlx_strdup(schemep);
if(!u->scheme) {
result = CURLUE_OUT_OF_MEMORY;
goto fail;
@ -1124,7 +1120,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
else
schemep = "http";
u->scheme = strdup(schemep);
u->scheme = curlx_strdup(schemep);
if(!u->scheme) {
result = CURLUE_OUT_OF_MEMORY;
goto fail;
@ -1197,7 +1193,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
}
else {
/* single byte query */
u->query = strdup("");
u->query = curlx_strdup("");
if(!u->query) {
result = CURLUE_OUT_OF_MEMORY;
goto fail;
@ -1241,7 +1237,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
goto fail;
}
if(dedot) {
free(u->path);
curlx_free(u->path);
u->path = dedot;
}
}
@ -1277,21 +1273,21 @@ static CURLUcode parseurl_and_replace(const char *url, CURLU *u,
*/
CURLU *curl_url(void)
{
return calloc(1, sizeof(struct Curl_URL));
return curlx_calloc(1, sizeof(struct Curl_URL));
}
void curl_url_cleanup(CURLU *u)
{
if(u) {
free_urlhandle(u);
free(u);
curlx_free(u);
}
}
#define DUP(dest, src, name) \
do { \
if(src->name) { \
dest->name = strdup(src->name); \
dest->name = curlx_strdup(src->name); \
if(!dest->name) \
goto fail; \
} \
@ -1299,7 +1295,7 @@ void curl_url_cleanup(CURLU *u)
CURLU *curl_url_dup(const CURLU *in)
{
struct Curl_URL *u = calloc(1, sizeof(struct Curl_URL));
struct Curl_URL *u = curlx_calloc(1, sizeof(struct Curl_URL));
if(u) {
DUP(u, in, scheme);
DUP(u, in, user);
@ -1373,7 +1369,7 @@ static CURLUcode urlget_format(const CURLU *u, CURLUPart what,
/* this unconditional rejection of control bytes is documented
API behavior */
CURLcode res = Curl_urldecode(part, partlen, &decoded, &dlen, REJECT_CTRL);
free(part);
curlx_free(part);
if(res)
return CURLUE_URLDECODE;
part = decoded;
@ -1383,7 +1379,7 @@ static CURLUcode urlget_format(const CURLU *u, CURLUPart what,
struct dynbuf enc;
curlx_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
uc = urlencode_str(&enc, part, partlen, TRUE, what == CURLUPART_QUERY);
free(part);
curlx_free(part);
if(uc)
return uc;
part = curlx_dyn_ptr(&enc);
@ -1392,7 +1388,7 @@ static CURLUcode urlget_format(const CURLU *u, CURLUPart what,
if(!Curl_is_ASCII_name(u->host)) {
char *punyversion = NULL;
uc = host_decode(part, &punyversion);
free(part);
curlx_free(part);
if(uc)
return uc;
part = punyversion;
@ -1402,7 +1398,7 @@ static CURLUcode urlget_format(const CURLU *u, CURLUPart what,
if(Curl_is_ASCII_name(u->host)) {
char *unpunified = NULL;
uc = host_encode(part, &unpunified);
free(part);
curlx_free(part);
if(uc)
return uc;
part = unpunified;
@ -1520,7 +1516,7 @@ static CURLUcode urlget_url(const CURLU *u, char **part, unsigned int flags)
u->query ? u->query : "",
show_fragment ? "#": "",
u->fragment ? u->fragment : "");
free(allochost);
curlx_free(allochost);
}
if(!url)
return CURLUE_OUT_OF_MEMORY;
@ -1666,7 +1662,7 @@ static CURLUcode set_url_port(CURLU *u, const char *provided_port)
tmp = curl_maprintf("%" CURL_FORMAT_CURL_OFF_T, port);
if(!tmp)
return CURLUE_OUT_OF_MEMORY;
free(u->port);
curlx_free(u->port);
u->port = tmp;
u->portnum = (unsigned short)port;
return CURLUE_OK;
@ -1691,7 +1687,7 @@ static CURLUcode set_url(CURLU *u, const char *url, size_t part_size,
if(!uc) {
/* success, meaning the "" is a fine relative URL, but nothing
changes */
free(oldurl);
curlx_free(oldurl);
return CURLUE_OK;
}
if(uc == CURLUE_OUT_OF_MEMORY)
@ -1715,7 +1711,7 @@ static CURLUcode set_url(CURLU *u, const char *url, size_t part_size,
DEBUGASSERT(oldurl); /* it is set here */
/* apply the relative part to create a new URL */
uc = redirect_url(oldurl, url, u, flags);
free(oldurl);
curlx_free(oldurl);
return uc;
}
@ -1930,7 +1926,7 @@ CURLUcode curl_url_set(CURLU *u, CURLUPart what,
if(curlx_dyn_add(&qbuf, newp))
goto nomem;
curlx_dyn_free(&enc);
free(*storep);
curlx_free(*storep);
*storep = curlx_dyn_ptr(&qbuf);
return CURLUE_OK;
nomem:
@ -1957,7 +1953,7 @@ nomem:
Curl_urldecode(newp, n, &decoded, &dlen, REJECT_CTRL);
if(result || hostname_check(u, decoded, dlen))
bad = TRUE;
free(decoded);
curlx_free(decoded);
}
else if(hostname_check(u, (char *)CURL_UNCONST(newp), n))
bad = TRUE;
@ -1968,7 +1964,7 @@ nomem:
}
}
free(*storep);
curlx_free(*storep);
*storep = (char *)CURL_UNCONST(newp);
}
return CURLUE_OK;