[OpenWrt-Devel] [PATCH] Deal with allocation errors in a few missing places.

Juliusz Chroboczek jch at pps.univ-paris-diderot.fr
Thu Jun 25 18:20:56 EDT 2015


Since libubox is used by procd, it should deal gracefully with allocation
failures.
---
 blobmsg_json.c |  2 ++
 json_script.c  |  3 +++
 kvlist.c       |  4 ++++
 ustream.c      | 16 +++++++++++++---
 utils.c        |  3 +++
 5 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/blobmsg_json.c b/blobmsg_json.c
index ffde23d..1f2b4bc 100644
--- a/blobmsg_json.c
+++ b/blobmsg_json.c
@@ -297,6 +297,8 @@ char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_jso
 
 	s.len = blob_len(attr);
 	s.buf = malloc(s.len);
+        if (!s.buf)
+            return NULL;
 	s.pos = 0;
 	s.custom_format = cb;
 	s.priv = priv;
diff --git a/json_script.c b/json_script.c
index 73c2502..8fe58b0 100644
--- a/json_script.c
+++ b/json_script.c
@@ -44,6 +44,9 @@ json_script_file_from_blobmsg(const char *name, void *data, int len)
 		name_len = strlen(name) + 1;
 
 	f = calloc_a(sizeof(*f) + len, &new_name, name_len);
+        if (!f)
+		return NULL;
+
 	memcpy(f->data, data, len);
 	if (name)
 		f->avl.key = strcpy(new_name, name);
diff --git a/kvlist.c b/kvlist.c
index e0a8acb..4097109 100644
--- a/kvlist.c
+++ b/kvlist.c
@@ -81,6 +81,10 @@ void kvlist_set(struct kvlist *kv, const char *name, const void *data)
 
 	node = calloc_a(sizeof(struct kvlist_node) + len,
 		&name_buf, strlen(name) + 1);
+        if (!node)
+            /* XXX */
+            return;
+
 	memcpy(node->data, data, len);
 
 	node->avl.key = strcpy(name_buf, name);
diff --git a/ustream.c b/ustream.c
index e7ee9f0..e051b68 100644
--- a/ustream.c
+++ b/ustream.c
@@ -65,6 +65,9 @@ static int ustream_alloc_default(struct ustream *s, struct ustream_buf_list *l)
 		return -1;
 
 	buf = malloc(sizeof(*buf) + l->buffer_len + s->string_data);
+	if (!buf)
+		return -1;
+
 	ustream_init_buf(buf, l->buffer_len);
 	ustream_add_buf(l, buf);
 
@@ -490,6 +493,8 @@ int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
 			return ustream_write_buffered(s, buf, maxlen, wr);
 		} else {
 			buf = malloc(maxlen + 1);
+			if (!buf)
+				return 0;
 			wr = vsnprintf(buf, maxlen + 1, format, arg);
 			wr = ustream_write(s, buf, wr, false);
 			free(buf);
@@ -507,20 +512,25 @@ int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
 	maxlen = vsnprintf(buf, buflen, format, arg2);
 	va_end(arg2);
 
+	buf = malloc(maxlen + 1);
+	if (!buf)
+		return 0;
+
 	wr = maxlen;
 	if (wr >= buflen)
 		wr = buflen - 1;
 
 	l->data_tail->tail += wr;
 	l->data_bytes += wr;
-	if (maxlen < buflen)
+	if (maxlen < buflen) {
+		free(buf);
 		return wr;
+	}
 
-	buf = malloc(maxlen + 1);
 	maxlen = vsnprintf(buf, maxlen + 1, format, arg);
 	wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
-	free(buf);
 
+	free(buf);
 	return wr;
 }
 
diff --git a/utils.c b/utils.c
index 8fd19f4..e9a1b69 100644
--- a/utils.c
+++ b/utils.c
@@ -43,6 +43,9 @@ void *__calloc_a(size_t len, ...)
 	va_end(ap1);
 
 	ptr = calloc(1, alloc_len);
+        if(ptr == NULL)
+		return NULL;
+
 	alloc_len = 0;
 	foreach_arg(ap, cur_addr, cur_len, &ret, len) {
 		*cur_addr = &ptr[alloc_len];
-- 
2.1.4
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list