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

@ -72,10 +72,6 @@
#include <fcntl.h>
#endif
/* The last 2 #include files should be in this order */
#include "../curl_memory.h"
#include "../memdebug.h"
/* A recent macro provided by libssh. Or make our own. */
#ifndef SSH_STRING_FREE_CHAR
#define SSH_STRING_FREE_CHAR(x) \
@ -491,10 +487,12 @@ static int myssh_is_known(struct Curl_easy *data, struct ssh_conn *sshc)
cleanup:
if(found_base64) {
(free)(found_base64);
/* !checksrc! disable BANNEDFUNC 1 */
free(found_base64); /* allocated by libssh, deallocate with system free */
}
if(known_base64) {
(free)(known_base64);
/* !checksrc! disable BANNEDFUNC 1 */
free(known_base64); /* allocated by libssh, deallocate with system free */
}
if(hash)
ssh_clean_pubkey_hash(&hash);
@ -605,7 +603,7 @@ static int myssh_in_SFTP_READDIR(struct Curl_easy *data,
}
result = Curl_client_write(data, CLIENTWRITE_BODY,
tmpLine, sshc->readdir_len + 1);
free(tmpLine);
curlx_free(tmpLine);
if(result) {
myssh_to(data, sshc, SSH_STOP);
@ -787,7 +785,7 @@ static int myssh_in_SFTP_QUOTE_STATVFS(struct Curl_easy *data,
if(!result) {
result = Curl_client_write(data, CLIENTWRITE_HEADER, tmp, strlen(tmp));
free(tmp);
curlx_free(tmp);
}
if(result) {
myssh_to(data, sshc, SSH_SFTP_CLOSE);
@ -1478,8 +1476,8 @@ static int myssh_in_SFTP_REALPATH(struct Curl_easy *data,
if(!sshc->homedir)
return myssh_to_ERROR(data, sshc, CURLE_COULDNT_CONNECT);
free(data->state.most_recent_ftp_entrypath);
data->state.most_recent_ftp_entrypath = strdup(sshc->homedir);
curlx_free(data->state.most_recent_ftp_entrypath);
data->state.most_recent_ftp_entrypath = curlx_strdup(sshc->homedir);
if(!data->state.most_recent_ftp_entrypath)
return myssh_to_ERROR(data, sshc, CURLE_OUT_OF_MEMORY);
@ -1577,7 +1575,7 @@ static int myssh_in_SFTP_QUOTE(struct Curl_easy *data,
the current directory can be read similar to how it is read when
using ordinary FTP. */
result = Curl_client_write(data, CLIENTWRITE_HEADER, tmp, strlen(tmp));
free(tmp);
curlx_free(tmp);
if(result) {
myssh_to(data, sshc, SSH_SFTP_CLOSE);
sshc->nextstate = SSH_NO_STATE;
@ -2529,7 +2527,7 @@ static void myssh_easy_dtor(void *key, size_t klen, void *entry)
(void)key;
(void)klen;
Curl_safefree(sshp->path);
free(sshp);
curlx_free(sshp);
}
static void myssh_conn_dtor(void *key, size_t klen, void *entry)
@ -2538,7 +2536,7 @@ static void myssh_conn_dtor(void *key, size_t klen, void *entry)
(void)key;
(void)klen;
sshc_cleanup(sshc);
free(sshc);
curlx_free(sshc);
}
/*
@ -2550,7 +2548,7 @@ static CURLcode myssh_setup_connection(struct Curl_easy *data,
struct SSHPROTO *sshp;
struct ssh_conn *sshc;
sshc = calloc(1, sizeof(*sshc));
sshc = curlx_calloc(1, sizeof(*sshc));
if(!sshc)
return CURLE_OUT_OF_MEMORY;
@ -2559,7 +2557,7 @@ static CURLcode myssh_setup_connection(struct Curl_easy *data,
if(Curl_conn_meta_set(conn, CURL_META_SSH_CONN, sshc, myssh_conn_dtor))
return CURLE_OUT_OF_MEMORY;
sshp = calloc(1, sizeof(*sshp));
sshp = curlx_calloc(1, sizeof(*sshp));
if(!sshp ||
Curl_meta_set(data, CURL_META_SSH_EASY, sshp, myssh_easy_dtor))
return CURLE_OUT_OF_MEMORY;

View file

@ -66,10 +66,6 @@
#include "../curlx/strparse.h"
#include "../curlx/base64.h" /* for base64 encoding/decoding */
/* The last 2 #include files should be in this order */
#include "../curl_memory.h"
#include "../memdebug.h"
/* Local functions: */
static const char *sftp_libssh2_strerror(unsigned long err);
static LIBSSH2_ALLOC_FUNC(my_libssh2_malloc);
@ -181,7 +177,7 @@ kbd_callback(const char *name, int name_len, const char *instruction,
#endif /* CURL_LIBSSH2_DEBUG */
if(num_prompts == 1) {
struct connectdata *conn = data->conn;
responses[0].text = strdup(conn->passwd);
responses[0].text = curlx_strdup(conn->passwd);
responses[0].length =
responses[0].text == NULL ? 0 : curlx_uztoui(strlen(conn->passwd));
}
@ -635,12 +631,12 @@ static CURLcode ssh_check_fingerprint(struct Curl_easy *data,
failf(data,
"Denied establishing ssh session: mismatch sha256 fingerprint. "
"Remote %s is not equal to %s", fingerprint_b64, pubkey_sha256);
free(fingerprint_b64);
curlx_free(fingerprint_b64);
myssh_state(data, sshc, SSH_SESSION_FREE);
return CURLE_PEER_FAILED_VERIFICATION;
}
free(fingerprint_b64);
curlx_free(fingerprint_b64);
infof(data, "SHA256 checksum match");
}
@ -881,7 +877,7 @@ static CURLcode sftp_quote(struct Curl_easy *data,
the current directory can be read similar to how it is read when
using ordinary FTP. */
result = Curl_client_write(data, CLIENTWRITE_HEADER, tmp, strlen(tmp));
free(tmp);
curlx_free(tmp);
if(!result)
myssh_state(data, sshc, SSH_SFTP_NEXT_QUOTE);
return result;
@ -1204,7 +1200,7 @@ static CURLcode ssh_state_pkey_init(struct Curl_easy *data,
sshc->rsa_pub = sshc->rsa = NULL;
if(data->set.str[STRING_SSH_PRIVATE_KEY])
sshc->rsa = strdup(data->set.str[STRING_SSH_PRIVATE_KEY]);
sshc->rsa = curlx_strdup(data->set.str[STRING_SSH_PRIVATE_KEY]);
else {
/* To ponder about: should really the lib be messing about with the
HOME environment variable etc? */
@ -1218,7 +1214,7 @@ static CURLcode ssh_state_pkey_init(struct Curl_easy *data,
if(!sshc->rsa)
out_of_memory = TRUE;
else if(curlx_stat(sshc->rsa, &sbuf)) {
free(sshc->rsa);
curlx_free(sshc->rsa);
sshc->rsa = curl_maprintf("%s/.ssh/id_dsa", home);
if(!sshc->rsa)
out_of_memory = TRUE;
@ -1226,19 +1222,19 @@ static CURLcode ssh_state_pkey_init(struct Curl_easy *data,
Curl_safefree(sshc->rsa);
}
}
free(home);
curlx_free(home);
}
if(!out_of_memory && !sshc->rsa) {
/* Nothing found; try the current dir. */
sshc->rsa = strdup("id_rsa");
sshc->rsa = curlx_strdup("id_rsa");
if(sshc->rsa && curlx_stat(sshc->rsa, &sbuf)) {
free(sshc->rsa);
sshc->rsa = strdup("id_dsa");
curlx_free(sshc->rsa);
sshc->rsa = curlx_strdup("id_dsa");
if(sshc->rsa && curlx_stat(sshc->rsa, &sbuf)) {
free(sshc->rsa);
curlx_free(sshc->rsa);
/* Out of guesses. Set to the empty string to avoid
* surprising info messages. */
sshc->rsa = strdup("");
sshc->rsa = curlx_strdup("");
}
}
}
@ -1252,7 +1248,7 @@ static CURLcode ssh_state_pkey_init(struct Curl_easy *data,
if(data->set.str[STRING_SSH_PUBLIC_KEY]
/* treat empty string the same way as NULL */
&& data->set.str[STRING_SSH_PUBLIC_KEY][0]) {
sshc->rsa_pub = strdup(data->set.str[STRING_SSH_PUBLIC_KEY]);
sshc->rsa_pub = curlx_strdup(data->set.str[STRING_SSH_PUBLIC_KEY]);
if(!sshc->rsa_pub)
out_of_memory = TRUE;
}
@ -1936,12 +1932,12 @@ static CURLcode ssh_state_sftp_realpath(struct Curl_easy *data,
myssh_state(data, sshc, SSH_STOP);
if(rc > 0) {
free(sshc->homedir);
sshc->homedir = strdup(sshp->readdir_filename);
curlx_free(sshc->homedir);
sshc->homedir = curlx_strdup(sshp->readdir_filename);
if(!sshc->homedir)
return CURLE_OUT_OF_MEMORY;
free(data->state.most_recent_ftp_entrypath);
data->state.most_recent_ftp_entrypath = strdup(sshc->homedir);
curlx_free(data->state.most_recent_ftp_entrypath);
data->state.most_recent_ftp_entrypath = curlx_strdup(sshc->homedir);
if(!data->state.most_recent_ftp_entrypath)
return CURLE_OUT_OF_MEMORY;
}
@ -2251,7 +2247,7 @@ static CURLcode ssh_state_sftp_quote_statvfs(struct Curl_easy *data,
}
result = Curl_client_write(data, CLIENTWRITE_HEADER, tmp, strlen(tmp));
free(tmp);
curlx_free(tmp);
if(result) {
myssh_state(data, sshc, SSH_SFTP_CLOSE);
sshc->nextstate = SSH_NO_STATE;
@ -3177,7 +3173,7 @@ static void myssh_easy_dtor(void *key, size_t klen, void *entry)
Curl_safefree(sshp->path);
curlx_dyn_free(&sshp->readdir);
curlx_dyn_free(&sshp->readdir_link);
free(sshp);
curlx_free(sshp);
}
static void myssh_conn_dtor(void *key, size_t klen, void *entry)
@ -3186,7 +3182,7 @@ static void myssh_conn_dtor(void *key, size_t klen, void *entry)
(void)key;
(void)klen;
sshc_cleanup(sshc, NULL, TRUE);
free(sshc);
curlx_free(sshc);
}
/*
@ -3199,14 +3195,14 @@ static CURLcode ssh_setup_connection(struct Curl_easy *data,
struct SSHPROTO *sshp;
(void)conn;
sshc = calloc(1, sizeof(*sshc));
sshc = curlx_calloc(1, sizeof(*sshc));
if(!sshc)
return CURLE_OUT_OF_MEMORY;
if(Curl_conn_meta_set(conn, CURL_META_SSH_CONN, sshc, myssh_conn_dtor))
return CURLE_OUT_OF_MEMORY;
sshp = calloc(1, sizeof(*sshp));
sshp = curlx_calloc(1, sizeof(*sshp));
if(!sshp)
return CURLE_OUT_OF_MEMORY;

View file

@ -30,9 +30,7 @@
#include <curl/curl.h>
#include "../curlx/strparse.h"
#include "../curl_trc.h"
#include "../curl_memory.h"
#include "../escape.h"
#include "../memdebug.h"
#define MAX_SSHPATH_LEN 100000 /* arbitrary */
@ -59,7 +57,7 @@ CURLcode Curl_getworkingpath(struct Curl_easy *data,
(working_path_len > 3) && (!memcmp(working_path, "/~/", 3))) {
/* It is referenced to the home directory, so strip the leading '/~/' */
if(curlx_dyn_addn(&npath, &working_path[3], working_path_len - 3)) {
free(working_path);
curlx_free(working_path);
return CURLE_OUT_OF_MEMORY;
}
}
@ -67,7 +65,7 @@ CURLcode Curl_getworkingpath(struct Curl_easy *data,
(!strcmp("/~", working_path) ||
((working_path_len > 2) && !memcmp(working_path, "/~/", 3)))) {
if(curlx_dyn_add(&npath, homedir)) {
free(working_path);
curlx_free(working_path);
return CURLE_OUT_OF_MEMORY;
}
if(working_path_len > 2) {
@ -82,20 +80,20 @@ CURLcode Curl_getworkingpath(struct Curl_easy *data,
if(curlx_dyn_addn(&npath, &working_path[copyfrom],
working_path_len - copyfrom)) {
free(working_path);
curlx_free(working_path);
return CURLE_OUT_OF_MEMORY;
}
}
else {
if(curlx_dyn_add(&npath, "/")) {
free(working_path);
curlx_free(working_path);
return CURLE_OUT_OF_MEMORY;
}
}
}
if(curlx_dyn_len(&npath)) {
free(working_path);
curlx_free(working_path);
/* store the pointer for the caller to receive */
*path = curlx_dyn_ptr(&npath);