From 7c992dd9f89ee219e7f3395211b8f7bdfebc35c3 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 8 Dec 2023 14:27:29 +0100 Subject: [PATCH] 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 --- lib/altsvc.c | 4 ++-- lib/bufref.c | 2 +- lib/cookie.c | 4 ++-- lib/formdata.c | 4 ++-- lib/ftp.c | 4 ++-- lib/hsts.c | 2 +- lib/http.c | 8 ++++---- lib/rtsp.c | 2 +- lib/strdup.c | 4 ++-- lib/strdup.h | 2 +- lib/urlapi.c | 8 ++++---- lib/vauth/ntlm_sspi.c | 2 +- lib/vssh/wolfssh.c | 2 +- lib/vtls/mbedtls.c | 4 ++-- lib/vtls/sectransp.c | 2 +- 15 files changed, 27 insertions(+), 27 deletions(-) diff --git a/lib/altsvc.c b/lib/altsvc.c index 35450d6b1c75..2d46b95c90be 100644 --- a/lib/altsvc.c +++ b/lib/altsvc.c @@ -123,11 +123,11 @@ static struct altsvc *altsvc_createid(const char *srchost, dlen -= 2; } - as->src.host = Curl_strndup(srchost, hlen); + as->src.host = Curl_memdup0(srchost, hlen); if(!as->src.host) goto error; - as->dst.host = Curl_strndup(dsthost, dlen); + as->dst.host = Curl_memdup0(dsthost, dlen); if(!as->dst.host) goto error; diff --git a/lib/bufref.c b/lib/bufref.c index 013eba10b2e8..f0a0e2a7dea1 100644 --- a/lib/bufref.c +++ b/lib/bufref.c @@ -117,7 +117,7 @@ CURLcode Curl_bufref_memdup(struct bufref *br, const void *ptr, size_t len) DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH); if(ptr) { - cpy = Curl_strndup(ptr, len); + cpy = Curl_memdup0(ptr, len); if(!cpy) return CURLE_OUT_OF_MEMORY; } diff --git a/lib/cookie.c b/lib/cookie.c index 156322a90467..1bd80ff5b3e5 100644 --- a/lib/cookie.c +++ b/lib/cookie.c @@ -365,7 +365,7 @@ static void strstore(char **str, const char *newstr, size_t len) DEBUGASSERT(newstr); DEBUGASSERT(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)); if(endslash) { size_t pathlen = (endslash-path + 1); /* include end slash */ - co->path = Curl_strndup(path, pathlen); + co->path = Curl_memdup0(path, pathlen); if(co->path) { co->spath = sanitize_cookie_path(co->path); if(!co->spath) diff --git a/lib/formdata.c b/lib/formdata.c index 81ed5fe61d04..8896b75e20d0 100644 --- a/lib/formdata.c +++ b/lib/formdata.c @@ -603,7 +603,7 @@ CURLFORMcode FormAdd(struct curl_httppost **httppost, app passed in a bad combo, so we better check for that first. */ if(form->name) { /* 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: strlen(form->name)); } @@ -779,7 +779,7 @@ static CURLcode setname(curl_mimepart *part, const char *name, size_t len) if(!name || !len) return curl_mime_name(part, name); - zname = Curl_strndup(name, len); + zname = Curl_memdup0(name, len); if(!zname) return CURLE_OUT_OF_MEMORY; res = curl_mime_name(part, zname); diff --git a/lib/ftp.c b/lib/ftp.c index ae98530e9224..87666dbcb23f 100644 --- a/lib/ftp.c +++ b/lib/ftp.c @@ -4170,7 +4170,7 @@ CURLcode ftp_parse_url_path(struct Curl_easy *data) return CURLE_OUT_OF_MEMORY; } - ftpc->dirs[0] = Curl_strndup(rawPath, dirlen); + ftpc->dirs[0] = Curl_memdup0(rawPath, dirlen); if(!ftpc->dirs[0]) { free(rawPath); 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 work on many servers and b) has no effect on the others. */ if(compLen > 0) { - char *comp = Curl_strndup(curPos, compLen); + char *comp = Curl_memdup0(curPos, compLen); if(!comp) { free(rawPath); return CURLE_OUT_OF_MEMORY; diff --git a/lib/hsts.c b/lib/hsts.c index 9314be294be9..99f5bd458fe5 100644 --- a/lib/hsts.c +++ b/lib/hsts.c @@ -135,7 +135,7 @@ static CURLcode hsts_create(struct hsts *h, if(!sts) return CURLE_OUT_OF_MEMORY; - duphost = Curl_strndup(hostname, hlen); + duphost = Curl_memdup0(hostname, hlen); if(!duphost) { free(sts); return CURLE_OUT_OF_MEMORY; diff --git a/lib/http.c b/lib/http.c index 2597937623ef..209780ed0a29 100644 --- a/lib/http.c +++ b/lib/http.c @@ -329,7 +329,7 @@ char *Curl_copy_header_value(const char *header) /* get length of the type */ len = end - start + 1; - return Curl_strndup(start, len); + return Curl_memdup0(start, len); } #ifndef CURL_DISABLE_HTTP_AUTH @@ -4617,17 +4617,17 @@ CURLcode Curl_http_req_make(struct httpreq **preq, goto out; memcpy(req->method, method, m_len); if(scheme) { - req->scheme = Curl_strndup(scheme, s_len); + req->scheme = Curl_memdup0(scheme, s_len); if(!req->scheme) goto out; } if(authority) { - req->authority = Curl_strndup(authority, a_len); + req->authority = Curl_memdup0(authority, a_len); if(!req->authority) goto out; } if(path) { - req->path = Curl_strndup(path, p_len); + req->path = Curl_memdup0(path, p_len); if(!req->path) goto out; } diff --git a/lib/rtsp.c b/lib/rtsp.c index f05ee727ebf3..f83c0b5886ac 100644 --- a/lib/rtsp.c +++ b/lib/rtsp.c @@ -934,7 +934,7 @@ CURLcode Curl_rtsp_parseheader(struct Curl_easy *data, char *header) */ /* 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]) return CURLE_OUT_OF_MEMORY; } diff --git a/lib/strdup.c b/lib/strdup.c index 50f0656b91be..299c9cc36b7a 100644 --- a/lib/strdup.c +++ b/lib/strdup.c @@ -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 '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. * ***************************************************************************/ -void *Curl_strndup(const char *src, size_t length) +void *Curl_memdup0(const char *src, size_t length) { char *buf = malloc(length + 1); if(!buf) diff --git a/lib/strdup.h b/lib/strdup.h index 9f12b25482a6..238a2611f645 100644 --- a/lib/strdup.h +++ b/lib/strdup.h @@ -33,6 +33,6 @@ wchar_t* Curl_wcsdup(const wchar_t* src); #endif void *Curl_memdup(const void *src, size_t buffer_length); 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 */ diff --git a/lib/urlapi.c b/lib/urlapi.c index 0d11e48c92a4..d8a6dab1abea 100644 --- a/lib/urlapi.c +++ b/lib/urlapi.c @@ -1231,7 +1231,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags) u->fragment = Curl_dyn_ptr(&enc); } else { - u->fragment = Curl_strndup(fragment + 1, fraglen - 1); + u->fragment = Curl_memdup0(fragment + 1, fraglen - 1); if(!u->fragment) { result = CURLUE_OUT_OF_MEMORY; goto fail; @@ -1260,7 +1260,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags) u->query = Curl_dyn_ptr(&enc); } else { - u->query = Curl_strndup(query + 1, qlen - 1); + u->query = Curl_memdup0(query + 1, qlen - 1); if(!u->query) { result = CURLUE_OUT_OF_MEMORY; goto fail; @@ -1294,7 +1294,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags) } else { if(!u->path) { - u->path = Curl_strndup(path, pathlen); + u->path = Curl_memdup0(path, pathlen); if(!u->path) { result = CURLUE_OUT_OF_MEMORY; goto fail; @@ -1592,7 +1592,7 @@ CURLUcode curl_url_get(const CURLU *u, CURLUPart what, if(ptr) { size_t partlen = strlen(ptr); size_t i = 0; - *part = Curl_strndup(ptr, partlen); + *part = Curl_memdup0(ptr, partlen); if(!*part) return CURLUE_OUT_OF_MEMORY; if(plusdecode) { diff --git a/lib/vauth/ntlm_sspi.c b/lib/vauth/ntlm_sspi.c index 06079a6ffc66..3d102eb4a4f5 100644 --- a/lib/vauth/ntlm_sspi.c +++ b/lib/vauth/ntlm_sspi.c @@ -214,7 +214,7 @@ CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data, } /* 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)); if(!ntlm->input_token) return CURLE_OUT_OF_MEMORY; diff --git a/lib/vssh/wolfssh.c b/lib/vssh/wolfssh.c index 8d152d43c73b..2fca32707770 100644 --- a/lib/vssh/wolfssh.c +++ b/lib/vssh/wolfssh.c @@ -513,7 +513,7 @@ static CURLcode wssh_statemach_act(struct Curl_easy *data, bool *block) return CURLE_OK; } 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) sshc->actualcode = CURLE_OUT_OF_MEMORY; wolfSSH_SFTPNAME_list_free(name); diff --git a/lib/vtls/mbedtls.c b/lib/vtls/mbedtls.c index f0fb7f3e4214..4734ce026a22 100644 --- a/lib/vtls/mbedtls.c +++ b/lib/vtls/mbedtls.c @@ -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 terminated even when provided the exact length, forcing us to waste 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); if(!newblob) 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 terminated even when provided the exact length, forcing us to waste 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); if(!newblob) return CURLE_OUT_OF_MEMORY; diff --git a/lib/vtls/sectransp.c b/lib/vtls/sectransp.c index 19c00e0350dd..075de0c88b89 100644 --- a/lib/vtls/sectransp.c +++ b/lib/vtls/sectransp.c @@ -2373,7 +2373,7 @@ static CURLcode verify_cert(struct Curl_cfilter *cf, if(ca_info_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); if(!certbuf) return CURLE_OUT_OF_MEMORY;