lib: rename Curl_strndup to Curl_memdup0 to avoid misunderstanding

Since the copy does not stop at a null byte, let's not call it anything
that makes you think it works like the common strndup() function.

Based on feedback from Jay Satiro, Stefan Eissing and Patrick Monnerat

Closes #12490
This commit is contained in:
Daniel Stenberg 2023-12-08 14:27:29 +01:00
parent 6d8dc2f636
commit 7c992dd9f8
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
15 changed files with 27 additions and 27 deletions

View File

@ -123,11 +123,11 @@ static struct altsvc *altsvc_createid(const char *srchost,
dlen -= 2; dlen -= 2;
} }
as->src.host = Curl_strndup(srchost, hlen); as->src.host = Curl_memdup0(srchost, hlen);
if(!as->src.host) if(!as->src.host)
goto error; goto error;
as->dst.host = Curl_strndup(dsthost, dlen); as->dst.host = Curl_memdup0(dsthost, dlen);
if(!as->dst.host) if(!as->dst.host)
goto error; goto error;

View File

@ -117,7 +117,7 @@ CURLcode Curl_bufref_memdup(struct bufref *br, const void *ptr, size_t len)
DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH); DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);
if(ptr) { if(ptr) {
cpy = Curl_strndup(ptr, len); cpy = Curl_memdup0(ptr, len);
if(!cpy) if(!cpy)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
} }

View File

@ -365,7 +365,7 @@ static void strstore(char **str, const char *newstr, size_t len)
DEBUGASSERT(newstr); DEBUGASSERT(newstr);
DEBUGASSERT(str); DEBUGASSERT(str);
free(*str); free(*str);
*str = Curl_strndup(newstr, len); *str = Curl_memdup0(newstr, len);
} }
/* /*
@ -821,7 +821,7 @@ Curl_cookie_add(struct Curl_easy *data,
endslash = memrchr(path, '/', (queryp - path)); endslash = memrchr(path, '/', (queryp - path));
if(endslash) { if(endslash) {
size_t pathlen = (endslash-path + 1); /* include end slash */ size_t pathlen = (endslash-path + 1); /* include end slash */
co->path = Curl_strndup(path, pathlen); co->path = Curl_memdup0(path, pathlen);
if(co->path) { if(co->path) {
co->spath = sanitize_cookie_path(co->path); co->spath = sanitize_cookie_path(co->path);
if(!co->spath) if(!co->spath)

View File

@ -603,7 +603,7 @@ CURLFORMcode FormAdd(struct curl_httppost **httppost,
app passed in a bad combo, so we better check for that first. */ app passed in a bad combo, so we better check for that first. */
if(form->name) { if(form->name) {
/* copy name (without strdup; possibly not null-terminated) */ /* copy name (without strdup; possibly not null-terminated) */
form->name = Curl_strndup(form->name, form->namelength? form->name = Curl_memdup0(form->name, form->namelength?
form->namelength: form->namelength:
strlen(form->name)); strlen(form->name));
} }
@ -779,7 +779,7 @@ static CURLcode setname(curl_mimepart *part, const char *name, size_t len)
if(!name || !len) if(!name || !len)
return curl_mime_name(part, name); return curl_mime_name(part, name);
zname = Curl_strndup(name, len); zname = Curl_memdup0(name, len);
if(!zname) if(!zname)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
res = curl_mime_name(part, zname); res = curl_mime_name(part, zname);

View File

@ -4170,7 +4170,7 @@ CURLcode ftp_parse_url_path(struct Curl_easy *data)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
} }
ftpc->dirs[0] = Curl_strndup(rawPath, dirlen); ftpc->dirs[0] = Curl_memdup0(rawPath, dirlen);
if(!ftpc->dirs[0]) { if(!ftpc->dirs[0]) {
free(rawPath); free(rawPath);
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
@ -4214,7 +4214,7 @@ CURLcode ftp_parse_url_path(struct Curl_easy *data)
CWD requires a parameter and a non-existent parameter a) doesn't CWD requires a parameter and a non-existent parameter a) doesn't
work on many servers and b) has no effect on the others. */ work on many servers and b) has no effect on the others. */
if(compLen > 0) { if(compLen > 0) {
char *comp = Curl_strndup(curPos, compLen); char *comp = Curl_memdup0(curPos, compLen);
if(!comp) { if(!comp) {
free(rawPath); free(rawPath);
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;

View File

@ -135,7 +135,7 @@ static CURLcode hsts_create(struct hsts *h,
if(!sts) if(!sts)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
duphost = Curl_strndup(hostname, hlen); duphost = Curl_memdup0(hostname, hlen);
if(!duphost) { if(!duphost) {
free(sts); free(sts);
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;

View File

@ -329,7 +329,7 @@ char *Curl_copy_header_value(const char *header)
/* get length of the type */ /* get length of the type */
len = end - start + 1; len = end - start + 1;
return Curl_strndup(start, len); return Curl_memdup0(start, len);
} }
#ifndef CURL_DISABLE_HTTP_AUTH #ifndef CURL_DISABLE_HTTP_AUTH
@ -4617,17 +4617,17 @@ CURLcode Curl_http_req_make(struct httpreq **preq,
goto out; goto out;
memcpy(req->method, method, m_len); memcpy(req->method, method, m_len);
if(scheme) { if(scheme) {
req->scheme = Curl_strndup(scheme, s_len); req->scheme = Curl_memdup0(scheme, s_len);
if(!req->scheme) if(!req->scheme)
goto out; goto out;
} }
if(authority) { if(authority) {
req->authority = Curl_strndup(authority, a_len); req->authority = Curl_memdup0(authority, a_len);
if(!req->authority) if(!req->authority)
goto out; goto out;
} }
if(path) { if(path) {
req->path = Curl_strndup(path, p_len); req->path = Curl_memdup0(path, p_len);
if(!req->path) if(!req->path)
goto out; goto out;
} }

View File

@ -934,7 +934,7 @@ CURLcode Curl_rtsp_parseheader(struct Curl_easy *data, char *header)
*/ */
/* Copy the id substring into a new buffer */ /* Copy the id substring into a new buffer */
data->set.str[STRING_RTSP_SESSION_ID] = Curl_strndup(start, idlen); data->set.str[STRING_RTSP_SESSION_ID] = Curl_memdup0(start, idlen);
if(!data->set.str[STRING_RTSP_SESSION_ID]) if(!data->set.str[STRING_RTSP_SESSION_ID])
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
} }

View File

@ -101,7 +101,7 @@ void *Curl_memdup(const void *src, size_t length)
/*************************************************************************** /***************************************************************************
* *
* Curl_strndup(source, length) * Curl_memdup0(source, length)
* *
* Copies the 'source' string to a newly allocated buffer (that is returned). * Copies the 'source' string to a newly allocated buffer (that is returned).
* Copies 'length' bytes then adds a null terminator. * Copies 'length' bytes then adds a null terminator.
@ -109,7 +109,7 @@ void *Curl_memdup(const void *src, size_t length)
* Returns the new pointer or NULL on failure. * Returns the new pointer or NULL on failure.
* *
***************************************************************************/ ***************************************************************************/
void *Curl_strndup(const char *src, size_t length) void *Curl_memdup0(const char *src, size_t length)
{ {
char *buf = malloc(length + 1); char *buf = malloc(length + 1);
if(!buf) if(!buf)

View File

@ -33,6 +33,6 @@ wchar_t* Curl_wcsdup(const wchar_t* src);
#endif #endif
void *Curl_memdup(const void *src, size_t buffer_length); void *Curl_memdup(const void *src, size_t buffer_length);
void *Curl_saferealloc(void *ptr, size_t size); void *Curl_saferealloc(void *ptr, size_t size);
void *Curl_strndup(const char *src, size_t length); void *Curl_memdup0(const char *src, size_t length);
#endif /* HEADER_CURL_STRDUP_H */ #endif /* HEADER_CURL_STRDUP_H */

View File

@ -1231,7 +1231,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
u->fragment = Curl_dyn_ptr(&enc); u->fragment = Curl_dyn_ptr(&enc);
} }
else { else {
u->fragment = Curl_strndup(fragment + 1, fraglen - 1); u->fragment = Curl_memdup0(fragment + 1, fraglen - 1);
if(!u->fragment) { if(!u->fragment) {
result = CURLUE_OUT_OF_MEMORY; result = CURLUE_OUT_OF_MEMORY;
goto fail; goto fail;
@ -1260,7 +1260,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
u->query = Curl_dyn_ptr(&enc); u->query = Curl_dyn_ptr(&enc);
} }
else { else {
u->query = Curl_strndup(query + 1, qlen - 1); u->query = Curl_memdup0(query + 1, qlen - 1);
if(!u->query) { if(!u->query) {
result = CURLUE_OUT_OF_MEMORY; result = CURLUE_OUT_OF_MEMORY;
goto fail; goto fail;
@ -1294,7 +1294,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
} }
else { else {
if(!u->path) { if(!u->path) {
u->path = Curl_strndup(path, pathlen); u->path = Curl_memdup0(path, pathlen);
if(!u->path) { if(!u->path) {
result = CURLUE_OUT_OF_MEMORY; result = CURLUE_OUT_OF_MEMORY;
goto fail; goto fail;
@ -1592,7 +1592,7 @@ CURLUcode curl_url_get(const CURLU *u, CURLUPart what,
if(ptr) { if(ptr) {
size_t partlen = strlen(ptr); size_t partlen = strlen(ptr);
size_t i = 0; size_t i = 0;
*part = Curl_strndup(ptr, partlen); *part = Curl_memdup0(ptr, partlen);
if(!*part) if(!*part)
return CURLUE_OUT_OF_MEMORY; return CURLUE_OUT_OF_MEMORY;
if(plusdecode) { if(plusdecode) {

View File

@ -214,7 +214,7 @@ CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
} }
/* Store the challenge for later use */ /* Store the challenge for later use */
ntlm->input_token = Curl_strndup((const char *)Curl_bufref_ptr(type2), ntlm->input_token = Curl_memdup0((const char *)Curl_bufref_ptr(type2),
Curl_bufref_len(type2)); Curl_bufref_len(type2));
if(!ntlm->input_token) if(!ntlm->input_token)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;

View File

@ -513,7 +513,7 @@ static CURLcode wssh_statemach_act(struct Curl_easy *data, bool *block)
return CURLE_OK; return CURLE_OK;
} }
else if(name && (rc == WS_SUCCESS)) { else if(name && (rc == WS_SUCCESS)) {
sshc->homedir = Curl_strndup(name->fName, name->fSz); sshc->homedir = Curl_memdup0(name->fName, name->fSz);
if(!sshc->homedir) if(!sshc->homedir)
sshc->actualcode = CURLE_OUT_OF_MEMORY; sshc->actualcode = CURLE_OUT_OF_MEMORY;
wolfSSH_SFTPNAME_list_free(name); wolfSSH_SFTPNAME_list_free(name);

View File

@ -368,7 +368,7 @@ mbed_connect_step1(struct Curl_cfilter *cf, struct Curl_easy *data)
/* Unfortunately, mbedtls_x509_crt_parse() requires the data to be null /* Unfortunately, mbedtls_x509_crt_parse() requires the data to be null
terminated even when provided the exact length, forcing us to waste terminated even when provided the exact length, forcing us to waste
extra memory here. */ extra memory here. */
unsigned char *newblob = Curl_strndup(ca_info_blob->data, unsigned char *newblob = Curl_memdup0(ca_info_blob->data,
ca_info_blob->len); ca_info_blob->len);
if(!newblob) if(!newblob)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
@ -441,7 +441,7 @@ mbed_connect_step1(struct Curl_cfilter *cf, struct Curl_easy *data)
/* Unfortunately, mbedtls_x509_crt_parse() requires the data to be null /* Unfortunately, mbedtls_x509_crt_parse() requires the data to be null
terminated even when provided the exact length, forcing us to waste terminated even when provided the exact length, forcing us to waste
extra memory here. */ extra memory here. */
unsigned char *newblob = Curl_strndup(ssl_cert_blob->data, unsigned char *newblob = Curl_memdup0(ssl_cert_blob->data,
ssl_cert_blob->len); ssl_cert_blob->len);
if(!newblob) if(!newblob)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;

View File

@ -2373,7 +2373,7 @@ static CURLcode verify_cert(struct Curl_cfilter *cf,
if(ca_info_blob) { if(ca_info_blob) {
CURL_TRC_CF(data, cf, "verify_peer, CA from config blob"); CURL_TRC_CF(data, cf, "verify_peer, CA from config blob");
certbuf = (unsigned char *)Curl_strndup(ca_info_blob->data, certbuf = (unsigned char *)Curl_memdup0(ca_info_blob->data,
buflen = ca_info_blob->len); buflen = ca_info_blob->len);
if(!certbuf) if(!certbuf)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;