curl/src/tool_libinfo.c
Viktor Szakats 193cb00ce9
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
2025-11-28 10:44:26 +01:00

211 lines
7.3 KiB
C

/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "tool_setup.h"
#include "tool_libinfo.h"
/* global variable definitions, for libcurl runtime info */
static const char *no_protos = NULL;
curl_version_info_data *curlinfo = NULL;
const char * const *built_in_protos = &no_protos;
size_t proto_count = 0;
const char *proto_file = NULL;
const char *proto_ftp = NULL;
const char *proto_ftps = NULL;
const char *proto_http = NULL;
const char *proto_https = NULL;
const char *proto_rtsp = NULL;
const char *proto_scp = NULL;
const char *proto_sftp = NULL;
const char *proto_tftp = NULL;
#ifndef CURL_DISABLE_IPFS
const char *proto_ipfs = "ipfs";
const char *proto_ipns = "ipns";
#endif /* !CURL_DISABLE_IPFS */
static struct proto_name_tokenp {
const char *proto_name;
const char **proto_tokenp;
} const possibly_built_in[] = {
{ "file", &proto_file },
{ "ftp", &proto_ftp },
{ "ftps", &proto_ftps },
{ "http", &proto_http },
{ "https", &proto_https },
{ "rtsp", &proto_rtsp },
{ "scp", &proto_scp },
{ "sftp", &proto_sftp },
{ "tftp", &proto_tftp },
{ NULL, NULL }
};
bool feature_altsvc = FALSE;
bool feature_brotli = FALSE;
bool feature_hsts = FALSE;
bool feature_http2 = FALSE;
bool feature_http3 = FALSE;
bool feature_httpsproxy = FALSE;
bool feature_libz = FALSE;
bool feature_libssh2 = FALSE;
bool feature_ntlm = FALSE;
bool feature_ntlm_wb = FALSE;
bool feature_spnego = FALSE;
bool feature_ssl = FALSE;
bool feature_tls_srp = FALSE;
bool feature_zstd = FALSE;
bool feature_ech = FALSE;
bool feature_ssls_export = FALSE;
static struct feature_name_presentp {
const char *feature_name;
bool *feature_presentp;
int feature_bitmask;
} const maybe_feature[] = {
/* Keep alphabetically sorted. */
{"alt-svc", &feature_altsvc, CURL_VERSION_ALTSVC},
{"AsynchDNS", NULL, CURL_VERSION_ASYNCHDNS},
{"brotli", &feature_brotli, CURL_VERSION_BROTLI},
{"CharConv", NULL, CURL_VERSION_CONV},
{"Debug", NULL, CURL_VERSION_DEBUG},
{"ECH", &feature_ech, 0},
{"gsasl", NULL, CURL_VERSION_GSASL},
{"GSS-API", NULL, CURL_VERSION_GSSAPI},
{"HSTS", &feature_hsts, CURL_VERSION_HSTS},
{"HTTP2", &feature_http2, CURL_VERSION_HTTP2},
{"HTTP3", &feature_http3, CURL_VERSION_HTTP3},
{"HTTPS-proxy", &feature_httpsproxy, CURL_VERSION_HTTPS_PROXY},
{"IDN", NULL, CURL_VERSION_IDN},
{"IPv6", NULL, CURL_VERSION_IPV6},
{"Kerberos", NULL, CURL_VERSION_KERBEROS5},
{"Largefile", NULL, CURL_VERSION_LARGEFILE},
{"libz", &feature_libz, CURL_VERSION_LIBZ},
{"MultiSSL", NULL, CURL_VERSION_MULTI_SSL},
{"NTLM", &feature_ntlm, CURL_VERSION_NTLM},
{"NTLM_WB", &feature_ntlm_wb, CURL_VERSION_NTLM_WB},
{"PSL", NULL, CURL_VERSION_PSL},
{"SPNEGO", &feature_spnego, CURL_VERSION_SPNEGO},
{"SSL", &feature_ssl, CURL_VERSION_SSL},
{"SSPI", NULL, CURL_VERSION_SSPI},
{"SSLS-EXPORT", &feature_ssls_export, 0},
{"threadsafe", NULL, CURL_VERSION_THREADSAFE},
{"TLS-SRP", &feature_tls_srp, CURL_VERSION_TLSAUTH_SRP},
{"TrackMemory", NULL, CURL_VERSION_CURLDEBUG},
{"Unicode", NULL, CURL_VERSION_UNICODE},
{"UnixSockets", NULL, CURL_VERSION_UNIX_SOCKETS},
{"zstd", &feature_zstd, CURL_VERSION_ZSTD},
{NULL, NULL, 0}
};
static const char *fnames[CURL_ARRAYSIZE(maybe_feature)];
const char * const *feature_names = fnames;
size_t feature_count;
/*
* libcurl_info_init: retrieves runtime information about libcurl,
* setting a global pointer 'curlinfo' to libcurl's runtime info
* struct, count protocols and flag those we are interested in.
* Global pointer feature_names is set to the feature names array. If
* the latter is not returned by curl_version_info(), it is built from
* the returned features bit mask.
*/
CURLcode get_libcurl_info(void)
{
CURLcode result = CURLE_OK;
const char *const *builtin;
/* Pointer to libcurl's runtime version information */
curlinfo = curl_version_info(CURLVERSION_NOW);
if(!curlinfo)
return CURLE_FAILED_INIT;
if(curlinfo->protocols) {
const struct proto_name_tokenp *p;
built_in_protos = curlinfo->protocols;
for(builtin = built_in_protos; !result && *builtin; builtin++) {
/* Identify protocols we are interested in. */
for(p = possibly_built_in; p->proto_name; p++)
if(curl_strequal(p->proto_name, *builtin)) {
*p->proto_tokenp = *builtin;
break;
}
}
proto_count = builtin - built_in_protos;
}
if(curlinfo->age >= CURLVERSION_ELEVENTH && curlinfo->feature_names)
feature_names = curlinfo->feature_names;
else {
const struct feature_name_presentp *p;
const char **cpp = fnames;
for(p = maybe_feature; p->feature_name; p++)
if(curlinfo->features & p->feature_bitmask)
*cpp++ = p->feature_name;
*cpp = NULL;
}
/* Identify features we are interested in. */
for(builtin = feature_names; *builtin; builtin++) {
const struct feature_name_presentp *p;
for(p = maybe_feature; p->feature_name; p++)
if(curl_strequal(p->feature_name, *builtin)) {
if(p->feature_presentp)
*p->feature_presentp = TRUE;
break;
}
++feature_count;
}
feature_libssh2 = curlinfo->libssh_version &&
!strncmp("libssh2", curlinfo->libssh_version, 7);
return CURLE_OK;
}
/* Tokenize a protocol name.
* Return the address of the protocol name listed by the library, or NULL if
* not found.
* Although this may seem useless, this always returns the same address for
* a given protocol and thus allows comparing pointers rather than strings.
* In addition, the returned pointer is not deallocated until the program ends.
*/
const char *proto_token(const char *proto)
{
const char * const *builtin;
if(!proto)
return NULL;
for(builtin = built_in_protos; *builtin; builtin++)
if(curl_strequal(*builtin, proto))
break;
return *builtin;
}