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

@ -25,7 +25,6 @@
#include "urldata.h"
#include "url.h" /* for Curl_safefree */
#include "memdebug.h" /* LAST include file */
struct etest {
const char *input;

View file

@ -25,7 +25,6 @@
#include "urldata.h"
#include "connect.h"
#include "memdebug.h" /* LAST include file */
static CURLcode t1303_setup(struct Curl_easy **easy)
{

View file

@ -23,7 +23,6 @@
***************************************************************************/
#include "unitcheck.h"
#include "netrc.h"
#include "memdebug.h" /* LAST include file */
#ifndef CURL_DISABLE_NETRC
@ -120,8 +119,8 @@ static CURLcode test_unit1304(const char *arg)
* Test for the first existing host in our netrc file
* with login[0] != 0.
*/
free(password);
free(login);
curlx_free(password);
curlx_free(login);
password = NULL;
login = NULL;
Curl_netrc_init(&store);
@ -139,9 +138,9 @@ static CURLcode test_unit1304(const char *arg)
* Test for the second existing host in our netrc file
* with login[0] = 0.
*/
free(password);
curlx_free(password);
password = NULL;
free(login);
curlx_free(login);
login = NULL;
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
@ -158,9 +157,9 @@ static CURLcode test_unit1304(const char *arg)
* Test for the second existing host in our netrc file
* with login[0] != 0.
*/
free(password);
curlx_free(password);
password = NULL;
free(login);
curlx_free(login);
login = NULL;
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,

View file

@ -36,8 +36,6 @@
#include "hash.h"
#include "hostip.h"
#include "memdebug.h" /* LAST include file */
static struct Curl_dnscache hp;
static char *data_key;
static struct Curl_dns_entry *data_node;
@ -52,9 +50,9 @@ static void t1305_stop(void)
{
if(data_node) {
Curl_freeaddrinfo(data_node->addr);
free(data_node);
curlx_free(data_node);
}
free(data_key);
curlx_free(data_key);
Curl_dnscache_destroy(&hp);
}
@ -64,8 +62,9 @@ static struct Curl_addrinfo *fake_ai(void)
static const char dummy[] = "dummy";
size_t namelen = sizeof(dummy); /* including the null-terminator */
ai = calloc(1, sizeof(struct Curl_addrinfo) + sizeof(struct sockaddr_in) +
namelen);
ai = curlx_calloc(1,
sizeof(struct Curl_addrinfo) + sizeof(struct sockaddr_in) +
namelen);
if(!ai)
return NULL;
@ -86,7 +85,7 @@ static CURLcode create_node(void)
if(!data_key)
return CURLE_OUT_OF_MEMORY;
data_node = calloc(1, sizeof(struct Curl_dns_entry));
data_node = curlx_calloc(1, sizeof(struct Curl_dns_entry));
if(!data_node)
return CURLE_OUT_OF_MEMORY;

View file

@ -23,13 +23,11 @@
***************************************************************************/
#include "unitcheck.h"
#include "memdebug.h"
static CURLcode test_unit1330(const char *arg)
{
UNITTEST_BEGIN_SIMPLE
char *ptr = malloc(1330);
char *ptr = curlx_malloc(1330);
Curl_safefree(ptr);
UNITTEST_END_SIMPLE

View file

@ -22,7 +22,6 @@
*
***************************************************************************/
#include "unitcheck.h"
#include "memdebug.h"
#include "unitprotos.h"
static CURLcode test_unit1395(const char *arg)
@ -133,7 +132,7 @@ static CURLcode test_unit1395(const char *arg)
}
else
curl_mfprintf(stderr, "Test %u: OK\n", i);
free(out);
curlx_free(out);
}
fail_if(fails, "output mismatched");

View file

@ -25,12 +25,10 @@
#include "hash.h"
#include "memdebug.h" /* LAST include file */
static void t1602_mydtor(void *p)
{
int *ptr = (int *)p;
free(ptr);
curlx_free(ptr);
}
static CURLcode t1602_setup(struct Curl_hash *hash)
@ -59,22 +57,22 @@ static CURLcode test_unit1602(const char *arg)
int key = 20;
int key2 = 25;
value = malloc(sizeof(int));
value = curlx_malloc(sizeof(int));
abort_unless(value != NULL, "Out of memory");
*value = 199;
nodep = Curl_hash_add(&hash, &key, klen, value);
if(!nodep)
free(value);
curlx_free(value);
abort_unless(nodep, "insertion into hash failed");
Curl_hash_clean(&hash);
/* Attempt to add another key/value pair */
value2 = malloc(sizeof(int));
value2 = curlx_malloc(sizeof(int));
abort_unless(value2 != NULL, "Out of memory");
*value2 = 204;
nodep = Curl_hash_add(&hash, &key2, klen, value2);
if(!nodep)
free(value2);
curlx_free(value2);
abort_unless(nodep, "insertion into hash failed");
UNITTEST_END(t1602_stop(&hash))

View file

@ -25,8 +25,6 @@
#include "hash.h"
#include "memdebug.h" /* LAST include file */
static const size_t slots = 3;
static void t1603_mydtor(void *p)

View file

@ -27,8 +27,6 @@
#include "connect.h"
#include "curl_share.h"
#include "memdebug.h" /* LAST include file */
static CURLcode t1607_setup(void)
{
CURLcode res = CURLE_OK;
@ -135,7 +133,7 @@ static CURLcode test_unit1607(const char *arg)
goto error;
dns = Curl_hash_pick(&multi->dnscache.entries,
entry_id, strlen(entry_id) + 1);
free(entry_id);
curlx_free(entry_id);
entry_id = NULL;
addr = dns ? dns->addr : NULL;

View file

@ -27,8 +27,6 @@
#include "connect.h"
#include "curl_share.h"
#include "memdebug.h" /* LAST include file */
static CURLcode t1609_setup(void)
{
CURLcode res = CURLE_OK;
@ -137,7 +135,7 @@ static CURLcode test_unit1609(const char *arg)
dns = Curl_hash_pick(&multi->dnscache.entries,
entry_id, strlen(entry_id) + 1);
free(entry_id);
curlx_free(entry_id);
entry_id = NULL;
addr = dns ? dns->addr : NULL;

View file

@ -25,13 +25,11 @@
#include "uint-hash.h"
#include "memdebug.h" /* LAST include file */
static void t1616_mydtor(unsigned int id, void *elem)
{
int *ptr = (int *)elem;
(void)id;
free(ptr);
curlx_free(ptr);
}
static CURLcode t1616_setup(struct uint_hash *hash)
@ -58,12 +56,12 @@ static CURLcode test_unit1616(const char *arg)
unsigned int key = 20;
unsigned int key2 = 25;
value = malloc(sizeof(int));
value = curlx_malloc(sizeof(int));
abort_unless(value != NULL, "Out of memory");
*value = 199;
ok = Curl_uint32_hash_set(&hash, key, value);
if(!ok)
free(value);
curlx_free(value);
abort_unless(ok, "insertion into hash failed");
v = Curl_uint32_hash_get(&hash, key);
abort_unless(v == value, "lookup present entry failed");
@ -72,12 +70,12 @@ static CURLcode test_unit1616(const char *arg)
Curl_uint32_hash_clear(&hash);
/* Attempt to add another key/value pair */
value2 = malloc(sizeof(int));
value2 = curlx_malloc(sizeof(int));
abort_unless(value2 != NULL, "Out of memory");
*value2 = 204;
ok = Curl_uint32_hash_set(&hash, key2, value2);
if(!ok)
free(value2);
curlx_free(value2);
abort_unless(ok, "insertion into hash failed");
v = Curl_uint32_hash_get(&hash, key2);
abort_unless(v == value2, "lookup present entry failed");

View file

@ -26,8 +26,6 @@
#include "urldata.h"
#include "url.h"
#include "memdebug.h" /* LAST include file */
static CURLcode t1620_setup(void)
{
CURLcode res = CURLE_OK;
@ -64,9 +62,9 @@ static void t1620_parse(
"options should be equal to exp_options");
}
free(userstr);
free(passwdstr);
free(options);
curlx_free(userstr);
curlx_free(passwdstr);
curlx_free(options);
}
static CURLcode test_unit1620(const char *arg)

View file

@ -53,7 +53,7 @@ static CURLcode test_unit1653(const char *arg)
u = curl_url();
if(!u)
goto fail;
ipv6port = strdup("[fe80::250:56ff:fea7:da15]");
ipv6port = curlx_strdup("[fe80::250:56ff:fea7:da15]");
if(!ipv6port)
goto fail;
ret = parse_port(u, ipv6port, FALSE);
@ -67,7 +67,7 @@ static CURLcode test_unit1653(const char *arg)
u = curl_url();
if(!u)
goto fail;
ipv6port = strdup("[fe80::250:56ff:fea7:da15|");
ipv6port = curlx_strdup("[fe80::250:56ff:fea7:da15|");
if(!ipv6port)
goto fail;
ret = parse_port(u, ipv6port, FALSE);
@ -78,7 +78,7 @@ static CURLcode test_unit1653(const char *arg)
u = curl_url();
if(!u)
goto fail;
ipv6port = strdup("[fe80::250:56ff;fea7:da15]:808");
ipv6port = curlx_strdup("[fe80::250:56ff;fea7:da15]:808");
if(!ipv6port)
goto fail;
ret = parse_port(u, ipv6port, FALSE);
@ -95,7 +95,7 @@ static CURLcode test_unit1653(const char *arg)
u = curl_url();
if(!u)
goto fail;
ipv6port = strdup("[fe80::250:56ff:fea7:da15%25eth3]:80");
ipv6port = curlx_strdup("[fe80::250:56ff:fea7:da15%25eth3]:80");
if(!ipv6port)
goto fail;
ret = parse_port(u, ipv6port, FALSE);
@ -111,7 +111,7 @@ static CURLcode test_unit1653(const char *arg)
u = curl_url();
if(!u)
goto fail;
ipv6port = strdup("[fe80::250:56ff:fea7:da15%25eth3]");
ipv6port = curlx_strdup("[fe80::250:56ff:fea7:da15%25eth3]");
if(!ipv6port)
goto fail;
ret = parse_port(u, ipv6port, FALSE);
@ -123,7 +123,7 @@ static CURLcode test_unit1653(const char *arg)
u = curl_url();
if(!u)
goto fail;
ipv6port = strdup("[fe80::250:56ff:fea7:da15]:81");
ipv6port = curlx_strdup("[fe80::250:56ff:fea7:da15]:81");
if(!ipv6port)
goto fail;
ret = parse_port(u, ipv6port, FALSE);
@ -139,7 +139,7 @@ static CURLcode test_unit1653(const char *arg)
u = curl_url();
if(!u)
goto fail;
ipv6port = strdup("[fe80::250:56ff:fea7:da15];81");
ipv6port = curlx_strdup("[fe80::250:56ff:fea7:da15];81");
if(!ipv6port)
goto fail;
ret = parse_port(u, ipv6port, FALSE);
@ -150,7 +150,7 @@ static CURLcode test_unit1653(const char *arg)
u = curl_url();
if(!u)
goto fail;
ipv6port = strdup("[fe80::250:56ff:fea7:da15]80");
ipv6port = curlx_strdup("[fe80::250:56ff:fea7:da15]80");
if(!ipv6port)
goto fail;
ret = parse_port(u, ipv6port, FALSE);
@ -163,7 +163,7 @@ static CURLcode test_unit1653(const char *arg)
u = curl_url();
if(!u)
goto fail;
ipv6port = strdup("[fe80::250:56ff:fea7:da15]:");
ipv6port = curlx_strdup("[fe80::250:56ff:fea7:da15]:");
if(!ipv6port)
goto fail;
ret = parse_port(u, ipv6port, TRUE);
@ -175,7 +175,7 @@ static CURLcode test_unit1653(const char *arg)
u = curl_url();
if(!u)
goto fail;
ipv6port = strdup("[fe80::250:56ff:fea7:da15!25eth3]:180");
ipv6port = curlx_strdup("[fe80::250:56ff:fea7:da15!25eth3]:180");
if(!ipv6port)
goto fail;
ret = parse_port(u, ipv6port, FALSE);
@ -191,7 +191,7 @@ static CURLcode test_unit1653(const char *arg)
u = curl_url();
if(!u)
goto fail;
ipv6port = strdup("[fe80::250:56ff:fea7:da15%eth3]:80");
ipv6port = curlx_strdup("[fe80::250:56ff:fea7:da15%eth3]:80");
if(!ipv6port)
goto fail;
ret = parse_port(u, ipv6port, FALSE);
@ -204,14 +204,14 @@ static CURLcode test_unit1653(const char *arg)
u = curl_url();
if(!u)
goto fail;
ipv6port = strdup("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaa:");
ipv6port = curlx_strdup("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaa:");
if(!ipv6port)
goto fail;
ret = parse_port(u, ipv6port, FALSE);
fail_unless(ret == CURLUE_BAD_PORT_NUMBER, "parse_port did wrong");
fail:
free(ipv6port);
curlx_free(ipv6port);
curl_url_cleanup(u);
UNITTEST_END_SIMPLE

View file

@ -23,7 +23,6 @@
***************************************************************************/
#include "unitcheck.h"
#include "bufref.h"
#include "memdebug.h"
static int freecount = 0;
@ -31,7 +30,7 @@ static void test_free(void *p)
{
fail_unless(p, "pointer to free may not be NULL");
freecount++;
free(p);
curlx_free(p);
}
static CURLcode t1661_setup(struct bufref *bufref)
@ -68,7 +67,7 @@ static CURLcode test_unit1661(const char *arg)
/**
* testing Curl_bufref_set
*/
buffer = malloc(13);
buffer = curlx_malloc(13);
abort_unless(buffer, "Out of memory");
Curl_bufref_set(&bufref, buffer, 13, test_free);

View file

@ -32,8 +32,6 @@
#include "cf-socket.h"
#include "memdebug.h" /* LAST include file */
static CURLcode t1663_setup(void)
{
CURLcode res = CURLE_OK;
@ -67,9 +65,9 @@ static void t1663_parse(
"host should be equal to exp_host");
}
free(dev);
free(iface);
free(host);
curlx_free(dev);
curlx_free(iface);
curlx_free(host);
}
static CURLcode test_unit1663(const char *arg)

View file

@ -30,8 +30,6 @@
#include <netinet/in6.h>
#endif
#include "memdebug.h" /* LAST include file */
static CURLcode t1664_setup(void)
{
CURLcode res = CURLE_OK;

View file

@ -47,7 +47,6 @@
#include "multiif.h"
#include "select.h"
#include "curl_trc.h"
#include "memdebug.h"
static CURLcode t2600_setup(CURL **easy)
{
@ -126,7 +125,7 @@ static void cf_test_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
#else
(void)data;
#endif
free(ctx);
curlx_free(ctx);
cf->ctx = NULL;
}
@ -193,7 +192,7 @@ static CURLcode cf_test_create(struct Curl_cfilter **pcf,
(void)data;
(void)conn;
ctx = calloc(1, sizeof(*ctx));
ctx = curlx_calloc(1, sizeof(*ctx));
if(!ctx) {
res = CURLE_OUT_OF_MEMORY;
goto out;
@ -233,8 +232,8 @@ static CURLcode cf_test_create(struct Curl_cfilter **pcf,
out:
*pcf = (!res) ? cf : NULL;
if(res) {
free(cf);
free(ctx);
curlx_free(cf);
curlx_free(ctx);
}
return res;
}

View file

@ -23,7 +23,6 @@
***************************************************************************/
#include "unitcheck.h"
#include "vssh/vssh.h"
#include "memdebug.h"
static CURLcode test_unit2604(const char *arg)
{
@ -79,7 +78,7 @@ static CURLcode test_unit2604(const char *arg)
#pragma GCC diagnostic pop
#endif
char *cp0 = calloc(1, too_long + 1);
char *cp0 = curlx_calloc(1, too_long + 1);
fail_unless(cp0, "could not alloc too long value");
memset(cp0, 'a', too_long);
@ -108,7 +107,7 @@ static CURLcode test_unit2604(const char *arg)
}
}
free(cp0);
curlx_free(cp0);
#endif

View file

@ -23,7 +23,6 @@
***************************************************************************/
#include "unitcheck.h"
#include "vssh/vssh.h"
#include "memdebug.h"
static CURLcode test_unit2605(const char *arg)
{

View file

@ -23,7 +23,6 @@
***************************************************************************/
#include "unitcheck.h"
#include "curl_get_line.h"
#include "memdebug.h"
static CURLcode test_unit3200(const char *arg)
{