From 3e9081921bac3a97f59699ffba34bcc4301ea2c2 Mon Sep 17 00:00:00 2001 From: defanor Date: Sat, 11 Nov 2023 23:40:53 +0300 Subject: Check realloc(3) return values --- src/rexmpp.c | 29 ++++++++++++++++++++++++++--- src/rexmpp_openpgp.c | 10 +++++++++- src/rexmpp_xml.c | 7 ++++++- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/rexmpp.c b/src/rexmpp.c index 7e4a212..d76b72a 100644 --- a/src/rexmpp.c +++ b/src/rexmpp.c @@ -147,7 +147,15 @@ char *rexmpp_capabilities_string (rexmpp_t *s, rexmpp_xml_t *info) { while (cur_len > buf_len - str_len) { buf_len *= 2; } - str = realloc(str, buf_len); + char *new_str = realloc(str, buf_len); + if (new_str == NULL) { + rexmpp_log(s, LOG_ERR, + "Failed to reallocate the capabilities string: %s", + strerror(errno)); + free(str); + return NULL; + } + str = new_str; } /* Fill the data. */ @@ -182,7 +190,15 @@ char *rexmpp_capabilities_string (rexmpp_t *s, rexmpp_xml_t *info) { while (cur_len > buf_len - str_len) { buf_len *= 2; } - str = realloc(str, buf_len); + char *new_str = realloc(str, buf_len); + if (new_str == NULL) { + rexmpp_log(s, LOG_ERR, + "Failed to reallocate the capabilities string: %s", + strerror(errno)); + free(str); + return NULL; + } + str = new_str; } strcpy(str + str_len, var); str_len += strlen(var); @@ -2232,7 +2248,14 @@ void rexmpp_sax_characters (rexmpp_t *s, const char *ch, size_t len) if (last_node != NULL && last_node->type == REXMPP_XML_TEXT) { /* The last child is textual as well, just extend it */ size_t last_len = strlen(last_node->alt.text); - last_node->alt.text = realloc(last_node->alt.text, last_len + len + 1); + char *new_alt_text = realloc(last_node->alt.text, last_len + len + 1); + if (new_alt_text == NULL) { + rexmpp_log(s, LOG_ERR, + "Failed to reallocate the XML element text buffer: %s", + strerror(errno)); + return; + } + last_node->alt.text = new_alt_text; strncpy(last_node->alt.text + last_len, ch, len); last_node->alt.text[last_len + len] = '\0'; } else { diff --git a/src/rexmpp_openpgp.c b/src/rexmpp_openpgp.c index 222ae6b..14e2d35 100644 --- a/src/rexmpp_openpgp.c +++ b/src/rexmpp_openpgp.c @@ -619,7 +619,15 @@ void rexmpp_openpgp_add_keys (rexmpp_t *s, *nkeys = *nkeys + 1; if (*nkeys == *allocated) { *allocated = *allocated * 2; - *keys = realloc(*keys, sizeof(gpgme_key_t *) * *allocated); + gpgme_key_t *new_keys = + realloc(*keys, sizeof(gpgme_key_t *) * *allocated); + if (new_keys == NULL) { + rexmpp_log(s, LOG_ERR, + "Failed to reallocate the OpenPGP keys array: %s", + strerror(errno)); + continue; + } + *keys = new_keys; } } else { gpgme_key_unref((*keys)[*nkeys]); diff --git a/src/rexmpp_xml.c b/src/rexmpp_xml.c index 9c3dd23..8b106ab 100644 --- a/src/rexmpp_xml.c +++ b/src/rexmpp_xml.c @@ -474,7 +474,12 @@ void rexmpp_xml_parse_sax_characters (struct rexmpp_xml_builder *builder, if (last_node != NULL && last_node->type == REXMPP_XML_TEXT) { /* The last child is textual as well, just extend it */ size_t last_len = strlen(last_node->alt.text); - last_node->alt.text = realloc(last_node->alt.text, last_len + len + 1); + char *new_alt_text = realloc(last_node->alt.text, last_len + len + 1); + if (new_alt_text == NULL) { + /* TODO: Would be nice a report an error here. */ + return; + } + last_node->alt.text = new_alt_text; strncpy(last_node->alt.text + last_len, ch, len); last_node->alt.text[last_len + len] = '\0'; } else { -- cgit v1.2.3