mirror of
https://github.com/curl/curl.git
synced 2026-05-06 10:17:29 +03:00
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 tob12da22db1#18866 Follow-up todb98daab05#18844 Follow-up to4deea9396b#18814 Follow-up to9678ff5b1b#18776 Follow-up to10bac43b87#18774 Follow-up to20142f5d06#18634 Follow-up tobf7375ecc5#18503 Follow-up to9863599d69#18502 Follow-up to3bb5e58c10#17827 Closes #19626
217 lines
5.6 KiB
C
217 lines
5.6 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 "curl_setup.h"
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
#include <netinet/in.h>
|
|
#endif
|
|
#ifdef HAVE_NETDB_H
|
|
#include <netdb.h>
|
|
#endif
|
|
#ifdef HAVE_ARPA_INET_H
|
|
#include <arpa/inet.h>
|
|
#endif
|
|
#ifdef __VMS
|
|
#include <in.h>
|
|
#include <inet.h>
|
|
#endif
|
|
|
|
#ifdef USE_ARES
|
|
#include <ares.h>
|
|
#include <ares_version.h> /* really old c-ares did not include this by
|
|
itself */
|
|
#endif
|
|
|
|
#include "urldata.h"
|
|
#include "asyn.h"
|
|
#include "sendf.h"
|
|
#include "hostip.h"
|
|
#include "hash.h"
|
|
#include "multiif.h"
|
|
#include "select.h"
|
|
#include "curl_share.h"
|
|
#include "url.h"
|
|
|
|
/***********************************************************************
|
|
* Only for builds using asynchronous name resolves
|
|
**********************************************************************/
|
|
#ifdef CURLRES_ASYNCH
|
|
|
|
|
|
#ifdef USE_ARES
|
|
|
|
#if ARES_VERSION < 0x010600
|
|
#error "requires c-ares 1.6.0 or newer"
|
|
#endif
|
|
|
|
/*
|
|
* Curl_ares_pollset() is called when the outside world (using
|
|
* curl_multi_fdset()) wants to get our fd_set setup and we are talking with
|
|
* ares. The caller must make sure that this function is only called when we
|
|
* have a working ares channel.
|
|
*
|
|
* Returns: sockets-in-use-bitmap
|
|
*/
|
|
CURLcode Curl_ares_pollset(struct Curl_easy *data,
|
|
ares_channel channel,
|
|
struct easy_pollset *ps)
|
|
{
|
|
struct timeval maxtime = { CURL_TIMEOUT_RESOLVE, 0 };
|
|
struct timeval timebuf;
|
|
curl_socket_t sockets[16]; /* ARES documented limit */
|
|
unsigned int bitmap, i;
|
|
struct timeval *timeout;
|
|
timediff_t milli;
|
|
CURLcode result = CURLE_OK;
|
|
|
|
DEBUGASSERT(channel);
|
|
if(!channel)
|
|
return CURLE_FAILED_INIT;
|
|
|
|
bitmap = ares_getsock(channel, (ares_socket_t *)sockets,
|
|
CURL_ARRAYSIZE(sockets));
|
|
for(i = 0; i < CURL_ARRAYSIZE(sockets); ++i) {
|
|
int flags = 0;
|
|
if(ARES_GETSOCK_READABLE(bitmap, i))
|
|
flags |= CURL_POLL_IN;
|
|
if(ARES_GETSOCK_WRITABLE(bitmap, i))
|
|
flags |= CURL_POLL_OUT;
|
|
if(!flags)
|
|
break;
|
|
result = Curl_pollset_change(data, ps, sockets[i], flags, 0);
|
|
if(result)
|
|
return result;
|
|
}
|
|
|
|
timeout = ares_timeout(channel, &maxtime, &timebuf);
|
|
if(!timeout)
|
|
timeout = &maxtime;
|
|
milli = curlx_tvtoms(timeout);
|
|
Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
|
|
return result;
|
|
}
|
|
|
|
/*
|
|
* Curl_ares_perform()
|
|
*
|
|
* 1) Ask ares what sockets it currently plays with, then
|
|
* 2) wait for the timeout period to check for action on ares' sockets.
|
|
* 3) tell ares to act on all the sockets marked as "with action"
|
|
*
|
|
* return number of sockets it worked on, or -1 on error
|
|
*/
|
|
int Curl_ares_perform(ares_channel channel,
|
|
timediff_t timeout_ms)
|
|
{
|
|
int nfds;
|
|
int bitmask;
|
|
ares_socket_t socks[ARES_GETSOCK_MAXNUM];
|
|
struct pollfd pfd[ARES_GETSOCK_MAXNUM];
|
|
int i;
|
|
int num = 0;
|
|
|
|
if(!channel)
|
|
return 0;
|
|
|
|
bitmask = ares_getsock(channel, socks, ARES_GETSOCK_MAXNUM);
|
|
|
|
for(i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
|
|
pfd[i].events = 0;
|
|
pfd[i].revents = 0;
|
|
if(ARES_GETSOCK_READABLE(bitmask, i)) {
|
|
pfd[i].fd = socks[i];
|
|
pfd[i].events |= POLLRDNORM|POLLIN;
|
|
}
|
|
if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
|
|
pfd[i].fd = socks[i];
|
|
pfd[i].events |= POLLWRNORM|POLLOUT;
|
|
}
|
|
if(pfd[i].events)
|
|
num++;
|
|
else
|
|
break;
|
|
}
|
|
|
|
if(num) {
|
|
nfds = Curl_poll(pfd, (unsigned int)num, timeout_ms);
|
|
if(nfds < 0)
|
|
return -1;
|
|
}
|
|
else
|
|
nfds = 0;
|
|
|
|
if(!nfds)
|
|
/* Call ares_process() unconditionally here, even if we simply timed out
|
|
above, as otherwise the ares name resolve will not timeout! */
|
|
ares_process_fd(channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
|
|
else {
|
|
/* move through the descriptors and ask for processing on them */
|
|
for(i = 0; i < num; i++)
|
|
ares_process_fd(channel,
|
|
(pfd[i].revents & (POLLRDNORM|POLLIN)) ?
|
|
pfd[i].fd : ARES_SOCKET_BAD,
|
|
(pfd[i].revents & (POLLWRNORM|POLLOUT)) ?
|
|
pfd[i].fd : ARES_SOCKET_BAD);
|
|
}
|
|
return nfds;
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* CURLRES_ASYNCH */
|
|
|
|
#ifdef USE_CURL_ASYNC
|
|
|
|
#include "doh.h"
|
|
|
|
void Curl_async_shutdown(struct Curl_easy *data)
|
|
{
|
|
#ifdef CURLRES_ARES
|
|
Curl_async_ares_shutdown(data);
|
|
#endif
|
|
#ifdef CURLRES_THREADED
|
|
Curl_async_thrdd_shutdown(data);
|
|
#endif
|
|
#ifndef CURL_DISABLE_DOH
|
|
Curl_doh_cleanup(data);
|
|
#endif
|
|
Curl_safefree(data->state.async.hostname);
|
|
}
|
|
|
|
void Curl_async_destroy(struct Curl_easy *data)
|
|
{
|
|
#ifdef CURLRES_ARES
|
|
Curl_async_ares_destroy(data);
|
|
#endif
|
|
#ifdef CURLRES_THREADED
|
|
Curl_async_thrdd_destroy(data);
|
|
#endif
|
|
#ifndef CURL_DISABLE_DOH
|
|
Curl_doh_cleanup(data);
|
|
#endif
|
|
Curl_safefree(data->state.async.hostname);
|
|
}
|
|
|
|
#endif /* USE_CURL_ASYNC */
|