From: Felix Fietkau Date: Fri, 21 Oct 2011 13:13:32 +0000 (+0200) Subject: add a few missing null pointer checks, and fix check vs dereference order in some... X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuci.git;a=commitdiff_plain;h=1d6095d5ac6482672dbb434b8df7c5de69b9d64e add a few missing null pointer checks, and fix check vs dereference order in some cases (patch by Stanislav Fomichev) --- diff --git a/cli.c b/cli.c index cfbf261..54f9484 100644 --- a/cli.c +++ b/cli.c @@ -450,7 +450,7 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv) ret = uci_add_list(ctx, &ptr); break; case CMD_REORDER: - if (!ptr.s) { + if (!ptr.s || !ptr.value) { ctx->err = UCI_ERR_NOTFOUND; cli_perror(); return 1; diff --git a/file.c b/file.c index 0530eb2..4eb35b1 100644 --- a/file.c +++ b/file.c @@ -619,7 +619,7 @@ int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct u uci_alloc_parse_context(ctx); pctx = ctx->pctx; pctx->file = stream; - if (*package && single) { + if (package && *package && single) { pctx->package = *package; pctx->merge = true; } diff --git a/libuci.c b/libuci.c index 995d39d..4c2bf96 100644 --- a/libuci.c +++ b/libuci.c @@ -177,7 +177,7 @@ uci_get_errorstr(struct uci_context *ctx, char **dest, const char *prefix) if (dest) { err = asprintf(dest, format, (prefix ? prefix : ""), (prefix ? ": " : ""), - (ctx->func ? ctx->func : ""), (ctx->func ? ": " : ""), + (ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""), uci_errstr[err], error_info); if (err < 0) @@ -186,7 +186,7 @@ uci_get_errorstr(struct uci_context *ctx, char **dest, const char *prefix) strcat(error_info, "\n"); fprintf(stderr, format, (prefix ? prefix : ""), (prefix ? ": " : ""), - (ctx->func ? ctx->func : ""), (ctx->func ? ": " : ""), + (ctx && ctx->func ? ctx->func : ""), (ctx && ctx->func ? ": " : ""), uci_errstr[err], error_info); } diff --git a/list.c b/list.c index fabad4d..cf6d970 100644 --- a/list.c +++ b/list.c @@ -485,7 +485,7 @@ static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, b int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr) { /* NB: UCI_INTERNAL use means without delta tracking */ - bool internal = ctx->internal; + bool internal = ctx && ctx->internal; struct uci_element *e; struct uci_package *p; char *n; @@ -530,7 +530,7 @@ int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos) int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res) { - bool internal = ctx->internal; + bool internal = ctx && ctx->internal; struct uci_section *s; UCI_HANDLE_ERR(ctx); @@ -547,7 +547,7 @@ int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char * int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr) { /* NB: pass on internal flag to uci_del_element */ - bool internal = ctx->internal; + bool internal = ctx && ctx->internal; struct uci_package *p; struct uci_element *e; @@ -574,7 +574,7 @@ int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr) int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr) { /* NB: UCI_INTERNAL use means without delta tracking */ - bool internal = ctx->internal; + bool internal = ctx && ctx->internal; struct uci_option *prev = NULL; const char *value2 = NULL; @@ -615,7 +615,7 @@ int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr) int uci_set(struct uci_context *ctx, struct uci_ptr *ptr) { /* NB: UCI_INTERNAL use means without delta tracking */ - bool internal = ctx->internal; + bool internal = ctx && ctx->internal; UCI_HANDLE_ERR(ctx); uci_expand_ptr(ctx, ptr, false); diff --git a/uci_internal.h b/uci_internal.h index e0c4204..02f81f5 100644 --- a/uci_internal.h +++ b/uci_internal.h @@ -186,9 +186,9 @@ struct uci_backend _var = { \ #define UCI_HANDLE_ERR(ctx) do { \ DPRINTF("ENTER: %s\n", __func__); \ int __val = 0; \ - ctx->err = 0; \ if (!ctx) \ return UCI_ERR_INVAL; \ + ctx->err = 0; \ if (!ctx->internal && !ctx->nested) \ __val = setjmp(ctx->trap); \ ctx->internal = false; \ diff --git a/ucimap-example.c b/ucimap-example.c index e04c66d..18c3c1f 100644 --- a/ucimap-example.c +++ b/ucimap-example.c @@ -132,6 +132,8 @@ static struct ucimap_section_data * network_allocate(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s) { struct uci_network *p = malloc(sizeof(struct uci_network)); + if (!p) + return NULL; memset(p, 0, sizeof(struct uci_network)); return &p->map; } @@ -244,6 +246,8 @@ int main(int argc, char **argv) INIT_LIST_HEAD(&ifs); ctx = uci_alloc_context(); + if (!ctx) + return -1; ucimap_init(&network_map); if ((argc >= 2) && !strcmp(argv[1], "-s")) { diff --git a/ucimap.c b/ucimap.c index 6a5c117..0bc31c5 100644 --- a/ucimap.c +++ b/ucimap.c @@ -295,6 +295,9 @@ realloc: offset = (items - (*list)->size) * sizeof(union ucimap_data); a->ptr = realloc(a->ptr, size); + if (!a->ptr) + return -ENOMEM; + if (offset) memset((char *) a->ptr + offset, 0, size - offset); new = a->ptr;