lib: add and use Curl_strndup()

The Curl_strndup() function is similar to memdup(), but copies 'n' bytes
then adds a terminating null byte ('\0').

Closes #12251
This commit is contained in:
Daniel Stenberg 2023-11-02 13:47:15 +01:00
parent fdaed85b02
commit d3b3ba35a5
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
7 changed files with 30 additions and 18 deletions

View file

@ -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_memdup(fragment + 1, fraglen);
u->fragment = Curl_strndup(fragment + 1, fraglen - 1);
if(!u->fragment) {
result = CURLUE_OUT_OF_MEMORY;
goto fail;
@ -1260,12 +1260,11 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
u->query = Curl_dyn_ptr(&enc);
}
else {
u->query = Curl_memdup(query + 1, qlen);
u->query = Curl_strndup(query + 1, qlen - 1);
if(!u->query) {
result = CURLUE_OUT_OF_MEMORY;
goto fail;
}
u->query[qlen - 1] = 0;
}
}
else {
@ -1295,12 +1294,11 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
}
else {
if(!u->path) {
u->path = Curl_memdup(path, pathlen + 1);
u->path = Curl_strndup(path, pathlen);
if(!u->path) {
result = CURLUE_OUT_OF_MEMORY;
goto fail;
}
u->path[pathlen] = 0;
path = u->path;
}
else if(flags & CURLU_URLENCODE)
@ -1594,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_memdup(ptr, partlen + 1);
*part = Curl_strndup(ptr, partlen);
if(!*part)
return CURLUE_OUT_OF_MEMORY;
if(plusdecode) {