build: tidy up and dedupe strdup functions

- de-dupe lib/src strdup/memdup functions into curlx.
- introduce `CURLX_STRDUP_LOW()` for mapping `strdup()`, and to do it at
  one place within the code, in `curl_setup.h`.
- tests/server: use `curlx_strdup()`. (Also to fix building without
  a system `strdup()`.)
- curlx/curlx.h: shorten and tidy up.
- adjust Windows build path to not need `HAVE_STRDUP`.
- build: stop detecting `HAVE_STRDUP` on Windows.

Closes #20497
This commit is contained in:
Viktor Szakats 2026-02-02 14:08:14 +01:00
parent e39650c984
commit 31a4f415af
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
50 changed files with 171 additions and 333 deletions

View file

@ -40,6 +40,7 @@ CURLX_CFILES = \
../lib/curlx/multibyte.c \
../lib/curlx/nonblock.c \
../lib/curlx/strcopy.c \
../lib/curlx/strdup.c \
../lib/curlx/strerr.c \
../lib/curlx/strparse.c \
../lib/curlx/timediff.c \
@ -58,6 +59,7 @@ CURLX_HFILES = \
../lib/curlx/nonblock.h \
../lib/curlx/snprintf.h \
../lib/curlx/strcopy.h \
../lib/curlx/strdup.h \
../lib/curlx/strerr.h \
../lib/curlx/strparse.h \
../lib/curlx/timediff.h \
@ -108,7 +110,6 @@ CURL_CFILES = \
tool_setopt.c \
tool_ssls.c \
tool_stderr.c \
tool_strdup.c \
tool_urlglob.c \
tool_util.c \
tool_vms.c \
@ -154,7 +155,6 @@ CURL_HFILES = \
tool_setup.h \
tool_ssls.h \
tool_stderr.h \
tool_strdup.h \
tool_urlglob.h \
tool_util.h \
tool_version.h \

View file

@ -34,7 +34,6 @@
#include "tool_cb_wrt.h"
#include "tool_operate.h"
#include "tool_libinfo.h"
#include "tool_strdup.h"
#ifdef _WIN32
#define BOLD "\x1b[1m"
@ -98,7 +97,7 @@ static void write_linked_location(CURL *curl, const char *location,
goto locout;
/* Create a null-terminated and whitespace-stripped copy of Location: */
copyloc = memdup0(loc, llen);
copyloc = curlx_memdup0(loc, llen);
if(!copyloc)
goto locout;
@ -154,7 +153,7 @@ static char *parse_filename(const char *ptr, size_t len, char stop)
char *p;
char *q;
copy = memdup0(ptr, len);
copy = curlx_memdup0(ptr, len);
if(!copy)
return NULL;

View file

@ -36,7 +36,6 @@
#include "tool_main.h"
#include "tool_stderr.h"
#include "tool_help.h"
#include "tool_strdup.h"
#include "var.h"
#define ALLOW_BLANK TRUE
@ -70,7 +69,7 @@ static ParameterError getstrn(char **str, const char *val,
if(!allowblank && !val[0])
return PARAM_BLANK_STRING;
*str = memdup0(val, len);
*str = curlx_memdup0(val, len);
if(!*str)
return PARAM_NO_MEM;

View file

@ -62,12 +62,6 @@ extern FILE *tool_stderr;
/* define what to use for unprintable characters */
#define UNPRINTABLE_CHAR '.'
#ifndef HAVE_STRDUP
#include "tool_strdup.h"
#undef Curl_strdup
#define Curl_strdup tool_strdup
#endif
#ifndef tool_nop_stmt
#define tool_nop_stmt do {} while(0)
#endif

View file

@ -1,55 +0,0 @@
/***************************************************************************
* _ _ ____ _
* 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_strdup.h"
#ifndef HAVE_STRDUP
char *tool_strdup(const char *str)
{
size_t len;
char *newstr;
if(!str)
return (char *)NULL;
len = strlen(str) + 1;
newstr = curlx_malloc(len);
if(!newstr)
return (char *)NULL;
memcpy(newstr, str, len);
return newstr;
}
#endif
char *memdup0(const char *data, size_t len)
{
char *p = curlx_malloc(len + 1);
if(!p)
return NULL;
if(len)
memcpy(p, data, len);
p[len] = 0;
return p;
}

View file

@ -1,33 +0,0 @@
#ifndef HEADER_TOOL_STRDUP_H
#define HEADER_TOOL_STRDUP_H
/***************************************************************************
* _ _ ____ _
* 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"
#ifndef HAVE_STRDUP
extern char *tool_strdup(const char *str);
#endif
char *memdup0(const char *data, size_t len);
#endif /* HEADER_TOOL_STRDUP_H */

View file

@ -27,7 +27,6 @@
#include "tool_doswin.h"
#include "tool_urlglob.h"
#include "tool_vms.h"
#include "tool_strdup.h"
static CURLcode globerror(struct URLGlob *glob, const char *err,
size_t pos, CURLcode error)
@ -49,7 +48,7 @@ static CURLcode glob_fixed(struct URLGlob *glob, char *fixed, size_t len)
if(!pat->c.set.elem)
return globerror(glob, NULL, 0, CURLE_OUT_OF_MEMORY);
pat->c.set.elem[0] = memdup0(fixed, len);
pat->c.set.elem[0] = curlx_memdup0(fixed, len);
if(!pat->c.set.elem[0]) {
tool_safefree(pat->c.set.elem);
return globerror(glob, NULL, 0, CURLE_OUT_OF_MEMORY);

View file

@ -28,7 +28,6 @@
#include "tool_msgs.h"
#include "tool_paramhlp.h"
#include "tool_writeout_json.h"
#include "tool_strdup.h"
#include "var.h"
#define MAX_EXPAND_CONTENT 10000000
@ -190,7 +189,7 @@ static ParameterError varfunc(char *c, /* content */
curlx_free(c);
clen = curlx_dyn_len(out);
c = memdup0(curlx_dyn_ptr(out), clen);
c = curlx_memdup0(curlx_dyn_ptr(out), clen);
if(!c) {
err = PARAM_NO_MEM;
break;
@ -357,7 +356,7 @@ static ParameterError addvariable(const char *name,
memcpy(p->name, name, nlen);
/* the null termination byte is already present from above */
p->content = contalloc ? content : memdup0(content, clen);
p->content = contalloc ? content : curlx_memdup0(content, clen);
if(p->content) {
p->clen = clen;