summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordefanor <defanor@uberspace.net>2023-11-11 23:40:53 +0300
committerdefanor <defanor@uberspace.net>2023-11-11 23:40:53 +0300
commit3e9081921bac3a97f59699ffba34bcc4301ea2c2 (patch)
tree1eed05cc9bafab03c125a5323d6d923b95f7bc6b
parentcafe83109a65b6bfd5bc0f3fd642e0833f5ebc78 (diff)
Check realloc(3) return values
-rw-r--r--src/rexmpp.c29
-rw-r--r--src/rexmpp_openpgp.c10
-rw-r--r--src/rexmpp_xml.c7
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 {