mirror of
https://github.com/curl/curl.git
synced 2026-04-19 07:41:14 +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
231 lines
6 KiB
C
231 lines
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
|
|
*
|
|
***************************************************************************/
|
|
|
|
/* Escape and unescape URL encoding in strings. The functions return a new
|
|
* allocated string or NULL if an error occurred. */
|
|
|
|
#include "curl_setup.h"
|
|
|
|
#include <curl/curl.h>
|
|
|
|
struct Curl_easy;
|
|
|
|
#include "urldata.h"
|
|
#include "curlx/warnless.h"
|
|
#include "escape.h"
|
|
#include "curlx/strparse.h"
|
|
#include "curl_printf.h"
|
|
|
|
/* for ABI-compatibility with previous versions */
|
|
char *curl_escape(const char *string, int inlength)
|
|
{
|
|
return curl_easy_escape(NULL, string, inlength);
|
|
}
|
|
|
|
/* for ABI-compatibility with previous versions */
|
|
char *curl_unescape(const char *string, int length)
|
|
{
|
|
return curl_easy_unescape(NULL, string, length, NULL);
|
|
}
|
|
|
|
/* Escapes for URL the given unescaped string of given length.
|
|
* 'data' is ignored since 7.82.0.
|
|
*/
|
|
char *curl_easy_escape(CURL *data, const char *string,
|
|
int inlength)
|
|
{
|
|
size_t length;
|
|
struct dynbuf d;
|
|
(void)data;
|
|
|
|
if(!string || (inlength < 0))
|
|
return NULL;
|
|
|
|
length = (inlength ? (size_t)inlength : strlen(string));
|
|
if(!length)
|
|
return curlx_strdup("");
|
|
|
|
curlx_dyn_init(&d, length * 3 + 1);
|
|
|
|
while(length--) {
|
|
/* treat the characters unsigned */
|
|
unsigned char in = (unsigned char)*string++;
|
|
|
|
if(ISUNRESERVED(in)) {
|
|
/* append this */
|
|
if(curlx_dyn_addn(&d, &in, 1))
|
|
return NULL;
|
|
}
|
|
else {
|
|
/* encode it */
|
|
unsigned char out[3]={'%'};
|
|
Curl_hexbyte(&out[1], in);
|
|
if(curlx_dyn_addn(&d, out, 3))
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
return curlx_dyn_ptr(&d);
|
|
}
|
|
|
|
/*
|
|
* Curl_urldecode() URL decodes the given string.
|
|
*
|
|
* Returns a pointer to a malloced string in *ostring with length given in
|
|
* *olen. If length == 0, the length is assumed to be strlen(string).
|
|
*
|
|
* ctrl options:
|
|
* - REJECT_NADA: accept everything
|
|
* - REJECT_CTRL: rejects control characters (byte codes lower than 32) in
|
|
* the data
|
|
* - REJECT_ZERO: rejects decoded zero bytes
|
|
*
|
|
* The values for the enum starts at 2, to make the assert detect legacy
|
|
* invokes that used TRUE/FALSE (0 and 1).
|
|
*/
|
|
|
|
CURLcode Curl_urldecode(const char *string, size_t length,
|
|
char **ostring, size_t *olen,
|
|
enum urlreject ctrl)
|
|
{
|
|
size_t alloc;
|
|
char *ns;
|
|
|
|
DEBUGASSERT(string);
|
|
DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */
|
|
|
|
alloc = (length ? length : strlen(string));
|
|
ns = curlx_malloc(alloc + 1);
|
|
|
|
if(!ns)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
/* store output string */
|
|
*ostring = ns;
|
|
|
|
while(alloc) {
|
|
unsigned char in = (unsigned char)*string;
|
|
if(('%' == in) && (alloc > 2) &&
|
|
ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
|
|
/* this is two hexadecimal digits following a '%' */
|
|
in = (unsigned char)((Curl_hexval(string[1]) << 4) |
|
|
Curl_hexval(string[2]));
|
|
string += 3;
|
|
alloc -= 3;
|
|
}
|
|
else {
|
|
string++;
|
|
alloc--;
|
|
}
|
|
|
|
if(((ctrl == REJECT_CTRL) && (in < 0x20)) ||
|
|
((ctrl == REJECT_ZERO) && (in == 0))) {
|
|
Curl_safefree(*ostring);
|
|
return CURLE_URL_MALFORMAT;
|
|
}
|
|
|
|
*ns++ = (char)in;
|
|
}
|
|
*ns = 0; /* terminate it */
|
|
|
|
if(olen)
|
|
/* store output size */
|
|
*olen = ns - *ostring;
|
|
|
|
return CURLE_OK;
|
|
}
|
|
|
|
/*
|
|
* Unescapes the given URL escaped string of given length. Returns a
|
|
* pointer to a malloced string with length given in *olen.
|
|
* If length == 0, the length is assumed to be strlen(string).
|
|
* If olen == NULL, no output length is stored.
|
|
* 'data' is ignored since 7.82.0.
|
|
*/
|
|
char *curl_easy_unescape(CURL *data, const char *string,
|
|
int length, int *olen)
|
|
{
|
|
char *str = NULL;
|
|
(void)data;
|
|
if(string && (length >= 0)) {
|
|
size_t inputlen = (size_t)length;
|
|
size_t outputlen;
|
|
CURLcode res = Curl_urldecode(string, inputlen, &str, &outputlen,
|
|
REJECT_NADA);
|
|
if(res)
|
|
return NULL;
|
|
|
|
if(olen) {
|
|
if(outputlen <= (size_t) INT_MAX)
|
|
*olen = curlx_uztosi(outputlen);
|
|
else
|
|
/* too large to return in an int, fail! */
|
|
Curl_safefree(str);
|
|
}
|
|
}
|
|
return str;
|
|
}
|
|
|
|
/* For operating systems/environments that use different malloc/free
|
|
systems for the app and for this library, we provide a free that uses
|
|
the library's memory system */
|
|
void curl_free(void *p)
|
|
{
|
|
curlx_free(p);
|
|
}
|
|
|
|
/*
|
|
* Curl_hexencode()
|
|
*
|
|
* Converts binary input to lowercase hex-encoded ASCII output.
|
|
* Null-terminated.
|
|
*/
|
|
void Curl_hexencode(const unsigned char *src, size_t len, /* input length */
|
|
unsigned char *out, size_t olen) /* output buffer size */
|
|
{
|
|
DEBUGASSERT(src && len && (olen >= 3));
|
|
if(src && len && (olen >= 3)) {
|
|
while(len-- && (olen >= 3)) {
|
|
out[0] = Curl_ldigits[*src >> 4];
|
|
out[1] = Curl_ldigits[*src & 0x0F];
|
|
++src;
|
|
out += 2;
|
|
olen -= 2;
|
|
}
|
|
*out = 0;
|
|
}
|
|
else if(olen)
|
|
*out = 0;
|
|
}
|
|
|
|
/* Curl_hexbyte
|
|
*
|
|
* Output a single unsigned char as a two-digit UPPERCASE hex number.
|
|
*/
|
|
void Curl_hexbyte(unsigned char *dest, /* must fit two bytes */
|
|
unsigned char val)
|
|
{
|
|
dest[0] = Curl_udigits[val >> 4];
|
|
dest[1] = Curl_udigits[val & 0x0F];
|
|
}
|