mirror of
https://github.com/curl/curl.git
synced 2026-06-16 09:05:40 +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
386 lines
10 KiB
C
386 lines
10 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"
|
|
#ifndef CURL_DISABLE_FTP
|
|
#include <curl/curl.h>
|
|
|
|
#include "curl_fnmatch.h"
|
|
|
|
#ifndef HAVE_FNMATCH
|
|
|
|
#define CURLFNM_CHARSET_LEN (sizeof(char) * 256)
|
|
#define CURLFNM_CHSET_SIZE (CURLFNM_CHARSET_LEN + 15)
|
|
|
|
#define CURLFNM_NEGATE CURLFNM_CHARSET_LEN
|
|
|
|
#define CURLFNM_ALNUM (CURLFNM_CHARSET_LEN + 1)
|
|
#define CURLFNM_DIGIT (CURLFNM_CHARSET_LEN + 2)
|
|
#define CURLFNM_XDIGIT (CURLFNM_CHARSET_LEN + 3)
|
|
#define CURLFNM_ALPHA (CURLFNM_CHARSET_LEN + 4)
|
|
#define CURLFNM_PRINT (CURLFNM_CHARSET_LEN + 5)
|
|
#define CURLFNM_BLANK (CURLFNM_CHARSET_LEN + 6)
|
|
#define CURLFNM_LOWER (CURLFNM_CHARSET_LEN + 7)
|
|
#define CURLFNM_GRAPH (CURLFNM_CHARSET_LEN + 8)
|
|
#define CURLFNM_SPACE (CURLFNM_CHARSET_LEN + 9)
|
|
#define CURLFNM_UPPER (CURLFNM_CHARSET_LEN + 10)
|
|
|
|
typedef enum {
|
|
CURLFNM_SCHS_DEFAULT = 0,
|
|
CURLFNM_SCHS_RIGHTBR,
|
|
CURLFNM_SCHS_RIGHTBRLEFTBR
|
|
} setcharset_state;
|
|
|
|
typedef enum {
|
|
CURLFNM_PKW_INIT = 0,
|
|
CURLFNM_PKW_DDOT
|
|
} parsekey_state;
|
|
|
|
typedef enum {
|
|
CCLASS_OTHER = 0,
|
|
CCLASS_DIGIT,
|
|
CCLASS_UPPER,
|
|
CCLASS_LOWER
|
|
} char_class;
|
|
|
|
#define SETCHARSET_OK 1
|
|
#define SETCHARSET_FAIL 0
|
|
|
|
static int parsekeyword(const unsigned char **pattern, unsigned char *charset)
|
|
{
|
|
parsekey_state state = CURLFNM_PKW_INIT;
|
|
char keyword[10] = { 0 };
|
|
size_t i;
|
|
const unsigned char *p = *pattern;
|
|
bool found = FALSE;
|
|
for(i = 0; !found; i++) {
|
|
char c = (char)*p++;
|
|
if(i >= sizeof(keyword))
|
|
return SETCHARSET_FAIL;
|
|
switch(state) {
|
|
case CURLFNM_PKW_INIT:
|
|
if(ISLOWER(c))
|
|
keyword[i] = c;
|
|
else if(c == ':')
|
|
state = CURLFNM_PKW_DDOT;
|
|
else
|
|
return SETCHARSET_FAIL;
|
|
break;
|
|
case CURLFNM_PKW_DDOT:
|
|
if(c == ']')
|
|
found = TRUE;
|
|
else
|
|
return SETCHARSET_FAIL;
|
|
}
|
|
}
|
|
#undef KEYLEN
|
|
|
|
*pattern = p; /* move caller's pattern pointer */
|
|
if(strcmp(keyword, "digit") == 0)
|
|
charset[CURLFNM_DIGIT] = 1;
|
|
else if(strcmp(keyword, "alnum") == 0)
|
|
charset[CURLFNM_ALNUM] = 1;
|
|
else if(strcmp(keyword, "alpha") == 0)
|
|
charset[CURLFNM_ALPHA] = 1;
|
|
else if(strcmp(keyword, "xdigit") == 0)
|
|
charset[CURLFNM_XDIGIT] = 1;
|
|
else if(strcmp(keyword, "print") == 0)
|
|
charset[CURLFNM_PRINT] = 1;
|
|
else if(strcmp(keyword, "graph") == 0)
|
|
charset[CURLFNM_GRAPH] = 1;
|
|
else if(strcmp(keyword, "space") == 0)
|
|
charset[CURLFNM_SPACE] = 1;
|
|
else if(strcmp(keyword, "blank") == 0)
|
|
charset[CURLFNM_BLANK] = 1;
|
|
else if(strcmp(keyword, "upper") == 0)
|
|
charset[CURLFNM_UPPER] = 1;
|
|
else if(strcmp(keyword, "lower") == 0)
|
|
charset[CURLFNM_LOWER] = 1;
|
|
else
|
|
return SETCHARSET_FAIL;
|
|
return SETCHARSET_OK;
|
|
}
|
|
|
|
/* Return the character class. */
|
|
static char_class charclass(unsigned char c)
|
|
{
|
|
if(ISUPPER(c))
|
|
return CCLASS_UPPER;
|
|
if(ISLOWER(c))
|
|
return CCLASS_LOWER;
|
|
if(ISDIGIT(c))
|
|
return CCLASS_DIGIT;
|
|
return CCLASS_OTHER;
|
|
}
|
|
|
|
/* Include a character or a range in set. */
|
|
static void setcharorrange(const unsigned char **pp, unsigned char *charset)
|
|
{
|
|
const unsigned char *p = (*pp)++;
|
|
unsigned char c = *p++;
|
|
|
|
charset[c] = 1;
|
|
if(ISALNUM(c) && *p++ == '-') {
|
|
char_class cc = charclass(c);
|
|
unsigned char endrange = *p++;
|
|
|
|
if(endrange == '\\')
|
|
endrange = *p++;
|
|
if(endrange >= c && charclass(endrange) == cc) {
|
|
while(c++ != endrange)
|
|
if(charclass(c) == cc) /* Chars in class may be not consecutive. */
|
|
charset[c] = 1;
|
|
*pp = p;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* returns 1 (TRUE) if pattern is OK, 0 if is bad ("p" is pattern pointer) */
|
|
static int setcharset(const unsigned char **p, unsigned char *charset)
|
|
{
|
|
setcharset_state state = CURLFNM_SCHS_DEFAULT;
|
|
bool something_found = FALSE;
|
|
unsigned char c;
|
|
|
|
memset(charset, 0, CURLFNM_CHSET_SIZE);
|
|
for(;;) {
|
|
c = **p;
|
|
if(!c)
|
|
return SETCHARSET_FAIL;
|
|
|
|
switch(state) {
|
|
case CURLFNM_SCHS_DEFAULT:
|
|
if(c == ']') {
|
|
if(something_found)
|
|
return SETCHARSET_OK;
|
|
something_found = TRUE;
|
|
state = CURLFNM_SCHS_RIGHTBR;
|
|
charset[c] = 1;
|
|
(*p)++;
|
|
}
|
|
else if(c == '[') {
|
|
const unsigned char *pp = *p + 1;
|
|
|
|
if(*pp++ == ':' && parsekeyword(&pp, charset))
|
|
*p = pp;
|
|
else {
|
|
charset[c] = 1;
|
|
(*p)++;
|
|
}
|
|
something_found = TRUE;
|
|
}
|
|
else if(c == '^' || c == '!') {
|
|
if(!something_found) {
|
|
if(charset[CURLFNM_NEGATE]) {
|
|
charset[c] = 1;
|
|
something_found = TRUE;
|
|
}
|
|
else
|
|
charset[CURLFNM_NEGATE] = 1; /* negate charset */
|
|
}
|
|
else
|
|
charset[c] = 1;
|
|
(*p)++;
|
|
}
|
|
else if(c == '\\') {
|
|
c = *(++(*p));
|
|
if(c)
|
|
setcharorrange(p, charset);
|
|
else
|
|
charset['\\'] = 1;
|
|
something_found = TRUE;
|
|
}
|
|
else {
|
|
setcharorrange(p, charset);
|
|
something_found = TRUE;
|
|
}
|
|
break;
|
|
case CURLFNM_SCHS_RIGHTBR:
|
|
if(c == '[') {
|
|
state = CURLFNM_SCHS_RIGHTBRLEFTBR;
|
|
charset[c] = 1;
|
|
(*p)++;
|
|
}
|
|
else if(c == ']') {
|
|
return SETCHARSET_OK;
|
|
}
|
|
else if(ISPRINT(c)) {
|
|
charset[c] = 1;
|
|
(*p)++;
|
|
state = CURLFNM_SCHS_DEFAULT;
|
|
}
|
|
else
|
|
/* used 'goto fail' instead of 'return SETCHARSET_FAIL' to avoid a
|
|
* nonsense warning 'statement not reached' at end of the fnc when
|
|
* compiling on Solaris */
|
|
goto fail;
|
|
break;
|
|
case CURLFNM_SCHS_RIGHTBRLEFTBR:
|
|
if(c == ']')
|
|
return SETCHARSET_OK;
|
|
state = CURLFNM_SCHS_DEFAULT;
|
|
charset[c] = 1;
|
|
(*p)++;
|
|
break;
|
|
}
|
|
}
|
|
fail:
|
|
return SETCHARSET_FAIL;
|
|
}
|
|
|
|
static int loop(const unsigned char *pattern, const unsigned char *string,
|
|
int maxstars)
|
|
{
|
|
const unsigned char *p = (const unsigned char *)pattern;
|
|
const unsigned char *s = (const unsigned char *)string;
|
|
unsigned char charset[CURLFNM_CHSET_SIZE] = { 0 };
|
|
|
|
for(;;) {
|
|
const unsigned char *pp;
|
|
|
|
switch(*p) {
|
|
case '*':
|
|
if(!maxstars)
|
|
return CURL_FNMATCH_NOMATCH;
|
|
/* Regroup consecutive stars and question marks. This can be done because
|
|
'*?*?*' can be expressed as '??*'. */
|
|
for(;;) {
|
|
if(*++p == '\0')
|
|
return CURL_FNMATCH_MATCH;
|
|
if(*p == '?') {
|
|
if(!*s++)
|
|
return CURL_FNMATCH_NOMATCH;
|
|
}
|
|
else if(*p != '*')
|
|
break;
|
|
}
|
|
/* Skip string characters until we find a match with pattern suffix. */
|
|
for(maxstars--; *s; s++) {
|
|
if(loop(p, s, maxstars) == CURL_FNMATCH_MATCH)
|
|
return CURL_FNMATCH_MATCH;
|
|
}
|
|
return CURL_FNMATCH_NOMATCH;
|
|
case '?':
|
|
if(!*s)
|
|
return CURL_FNMATCH_NOMATCH;
|
|
s++;
|
|
p++;
|
|
break;
|
|
case '\0':
|
|
return *s ? CURL_FNMATCH_NOMATCH : CURL_FNMATCH_MATCH;
|
|
case '\\':
|
|
if(p[1])
|
|
p++;
|
|
if(*s++ != *p++)
|
|
return CURL_FNMATCH_NOMATCH;
|
|
break;
|
|
case '[':
|
|
pp = p + 1; /* Copy in case of syntax error in set. */
|
|
if(setcharset(&pp, charset)) {
|
|
bool found = FALSE;
|
|
if(!*s)
|
|
return CURL_FNMATCH_NOMATCH;
|
|
if(charset[(unsigned int)*s])
|
|
found = TRUE;
|
|
else if(charset[CURLFNM_ALNUM])
|
|
found = ISALNUM(*s);
|
|
else if(charset[CURLFNM_ALPHA])
|
|
found = ISALPHA(*s);
|
|
else if(charset[CURLFNM_DIGIT])
|
|
found = ISDIGIT(*s);
|
|
else if(charset[CURLFNM_XDIGIT])
|
|
found = ISXDIGIT(*s);
|
|
else if(charset[CURLFNM_PRINT])
|
|
found = ISPRINT(*s);
|
|
else if(charset[CURLFNM_SPACE])
|
|
found = ISBLANK(*s);
|
|
else if(charset[CURLFNM_UPPER])
|
|
found = ISUPPER(*s);
|
|
else if(charset[CURLFNM_LOWER])
|
|
found = ISLOWER(*s);
|
|
else if(charset[CURLFNM_BLANK])
|
|
found = ISBLANK(*s);
|
|
else if(charset[CURLFNM_GRAPH])
|
|
found = ISGRAPH(*s);
|
|
|
|
if(charset[CURLFNM_NEGATE])
|
|
found = !found;
|
|
|
|
if(!found)
|
|
return CURL_FNMATCH_NOMATCH;
|
|
p = pp + 1;
|
|
s++;
|
|
break;
|
|
}
|
|
/* Syntax error in set; mismatch! */
|
|
return CURL_FNMATCH_NOMATCH;
|
|
|
|
default:
|
|
if(*p++ != *s++)
|
|
return CURL_FNMATCH_NOMATCH;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
* @unittest: 1307
|
|
*/
|
|
int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
|
|
{
|
|
(void)ptr; /* the argument is specified by the curl_fnmatch_callback
|
|
prototype, but not used by Curl_fnmatch() */
|
|
if(!pattern || !string) {
|
|
return CURL_FNMATCH_FAIL;
|
|
}
|
|
return loop((const unsigned char *)pattern,
|
|
(const unsigned char *)string, 2);
|
|
}
|
|
#else
|
|
#include <fnmatch.h>
|
|
/*
|
|
* @unittest: 1307
|
|
*/
|
|
int Curl_fnmatch(void *ptr, const char *pattern, const char *string)
|
|
{
|
|
(void)ptr; /* the argument is specified by the curl_fnmatch_callback
|
|
prototype, but not used by Curl_fnmatch() */
|
|
if(!pattern || !string) {
|
|
return CURL_FNMATCH_FAIL;
|
|
}
|
|
|
|
switch(fnmatch(pattern, string, 0)) {
|
|
case 0:
|
|
return CURL_FNMATCH_MATCH;
|
|
case FNM_NOMATCH:
|
|
return CURL_FNMATCH_NOMATCH;
|
|
default:
|
|
return CURL_FNMATCH_FAIL;
|
|
}
|
|
/* not reached */
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* if FTP is disabled */
|