From ef95525edc1ade7e729187b011de889b8f5f57c6 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Fri, 21 Oct 2011 15:12:04 +0200 Subject: [PATCH] check result of memory allocations (patch by Stanislav Fomichev) --- cli.c | 8 +++++++- file.c | 5 ++++- ucimap.c | 2 ++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/cli.c b/cli.c index b660273..cfbf261 100644 --- a/cli.c +++ b/cli.c @@ -98,6 +98,8 @@ uci_lookup_section_ref(struct uci_section *s) } if (!ti) { ti = malloc(sizeof(struct uci_type_list)); + if (!ti) + return NULL; memset(ti, 0, sizeof(struct uci_type_list)); ti->next = type_list; type_list = ti; @@ -110,8 +112,12 @@ uci_lookup_section_ref(struct uci_section *s) } else { typestr = realloc(typestr, maxlen); } - sprintf(typestr, "@%s[%d]", ti->name, ti->idx); + + if (typestr) + sprintf(typestr, "@%s[%d]", ti->name, ti->idx); + ti->idx++; + return typestr; } diff --git a/file.c b/file.c index d23e910..0530eb2 100644 --- a/file.c +++ b/file.c @@ -508,7 +508,7 @@ invalid: /* * escape an uci string for export */ -static char *uci_escape(struct uci_context *ctx, const char *str) +static const char *uci_escape(struct uci_context *ctx, const char *str) { const char *end; int ofs = 0; @@ -516,6 +516,9 @@ static char *uci_escape(struct uci_context *ctx, const char *str) if (!ctx->buf) { ctx->bufsz = LINEBUF; ctx->buf = malloc(LINEBUF); + + if (!ctx->buf) + return str; } while (1) { diff --git a/ucimap.c b/ucimap.c index 16ce56a..6a5c117 100644 --- a/ucimap.c +++ b/ucimap.c @@ -275,6 +275,8 @@ ucimap_resize_list(struct ucimap_section_data *sd, struct ucimap_list **list, in if (!*list) { new = calloc(1, size); + if (!new) + return -ENOMEM; ucimap_add_alloc(sd, new); goto set; -- 2.11.0