Fix various memory management issues

Consistently handle allocation failures. Some functions are changed to
return bool or int instead of void to allow returning an error.

Also fix a buffer size miscalculation in lua/uloop and use _exit() instead
of exit() on errors after forking.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
This commit is contained in:
Matthias Schiffer 2016-06-21 17:19:10 +02:00 committed by Felix Fietkau
parent c2f2c47f3e
commit 1f019ceea1
10 changed files with 83 additions and 23 deletions

View file

@ -119,15 +119,22 @@ struct strbuf {
static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
{
size_t new_len;
char *new_buf;
if (len <= 0)
return true;
if (s->pos + len >= s->len) {
s->len += 16 + len;
s->buf = realloc(s->buf, s->len);
if (!s->buf)
new_len = s->len + 16 + len;
new_buf = realloc(s->buf, new_len);
if (!new_buf)
return false;
s->len = new_len;
s->buf = new_buf;
}
memcpy(s->buf + s->pos, c, len);
s->pos += len;
return true;
@ -290,14 +297,18 @@ char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_jso
{
struct strbuf s;
bool array;
char *ret;
s.len = blob_len(attr);
s.buf = malloc(s.len);
s.pos = 0;
s.custom_format = cb;
s.priv = priv;
s.indent = false;
s.buf = malloc(s.len);
if (!s.buf)
return NULL;
if (indent >= 0) {
s.indent = true;
s.indent_level = indent;
@ -316,8 +327,13 @@ char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_jso
return NULL;
}
s.buf = realloc(s.buf, s.pos + 1);
s.buf[s.pos] = 0;
ret = realloc(s.buf, s.pos + 1);
if (!ret) {
free(s.buf);
return NULL;
}
return s.buf;
ret[s.pos] = 0;
return ret;
}