From 861298fa2467c3bf0c2893b2b675aa2ab836e0a2 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sun, 23 Apr 2023 11:48:47 +0200 Subject: [PATCH] avoid realloc(p, 0) whose behavior is implementation-defined fixes #652 see https://github.com/miniupnp/miniupnp/issues/652#issuecomment-1518922139 --- miniupnpd/upnppermissions.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/miniupnpd/upnppermissions.c b/miniupnpd/upnppermissions.c index 20058e1..a2dec74 100644 --- a/miniupnpd/upnppermissions.c +++ b/miniupnpd/upnppermissions.c @@ -152,7 +152,22 @@ get_next_token(const char * s, char ** token, int raw) memmove(*token + i + 1, *token + 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 */