netrc: deal with null token better

If there is no length of the token, assign a blank string to avoid
risking it being NULL.

Pointed out by Coverity

Closes #17351
This commit is contained in:
Daniel Stenberg 2025-05-15 00:05:11 +02:00
parent 473b3e51d6
commit f5b7eb27f0
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2

View File

@ -220,7 +220,12 @@ static NETRCcode parsenetrc(struct store_netrc *store,
}
}
tok = curlx_dyn_ptr(&token);
if(curlx_dyn_len(&token))
tok = curlx_dyn_ptr(&token);
else
/* since tok might actually be NULL for no content, set it to blank
to avoid having to deal with it being NULL */
tok = "";
switch(state) {
case NOTHING:
@ -247,7 +252,7 @@ static NETRCcode parsenetrc(struct store_netrc *store,
}
break;
case MACDEF:
if(!tok || !*tok)
if(!*tok)
state = NOTHING;
break;
case HOSTFOUND:
@ -268,7 +273,7 @@ static NETRCcode parsenetrc(struct store_netrc *store,
else {
our_login = TRUE;
free(login);
login = strdup(tok ? tok : "");
login = strdup(tok);
if(!login) {
retcode = NETRC_OUT_OF_MEMORY; /* allocation failed */
goto out;