avoid realloc(p, 0) whose behavior is implementation-defined

fixes #652

see https://github.com/miniupnp/miniupnp/issues/652#issuecomment-1518922139
This commit is contained in:
Thomas Bernard 2023-04-23 11:48:47 +02:00
parent 5ca1a82f7f
commit 861298fa24
No known key found for this signature in database
GPG Key ID: DB511043A31ACAAF
1 changed files with 16 additions and 1 deletions

View File

@ -152,7 +152,22 @@ get_next_token(const char * s, char ** token, int raw)
memmove(*token + i + 1, *token + i + sequence_len, memmove(*token + i + 1, *token + i + sequence_len,
token_len - i - sequence_len); token_len - i - sequence_len);
} }
*token = realloc(*token, i); if (i == 0)
{
/* behavior of realloc(p, 0) is implementation-defined, so better set it to NULL.
* https://github.com/miniupnp/miniupnp/issues/652#issuecomment-1518922139 */
free(*token);
*token = NULL;
}
else
{
char * tmp = realloc(*token, i);
if (tmp != NULL)
*token = tmp;
else
syslog(LOG_ERR, "%s: failed to reallocate to %u bytes",
"get_next_token()", i);
}
} }
/* return the beginning of the next token */ /* return the beginning of the next token */