X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=ucimap.c;h=b4f9518f5b5af5a4d7d294389b650d6576f71a4d;hp=b477264b23dca1b905784bddb8fd6d32de2d9ad0;hb=c4df32b386c7bb29568140d135d7315e76c934b7;hpb=c81b25ffd34632dc6ac56fe61faaf44eca822259 diff --git a/ucimap.c b/ucimap.c index b477264..b4f9518 100644 --- a/ucimap.c +++ b/ucimap.c @@ -9,11 +9,11 @@ * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * GNU Lesser General Public License for more details. */ /* - * This file contains ucimap, an API for mapping UCI to C data structures + * This file contains ucimap, an API for mapping UCI to C data structures */ #include @@ -59,23 +59,13 @@ struct ucimap_fixup { static inline bool ucimap_is_alloc(enum ucimap_type type) { - switch(type & UCIMAP_SUBTYPE) { - case UCIMAP_STRING: - return true; - default: - return false; - } + return (type & UCIMAP_SUBTYPE) == UCIMAP_STRING; } static inline bool ucimap_is_fixup(enum ucimap_type type) { - switch(type & UCIMAP_SUBTYPE) { - case UCIMAP_SECTION: - return true; - default: - return false; - } + return (type & UCIMAP_SUBTYPE) == UCIMAP_SECTION; } static inline bool @@ -172,9 +162,10 @@ ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd) void ucimap_cleanup(struct uci_map *map) { - struct ucimap_section_data *sd; + struct ucimap_section_data *sd, *sd_next; - for (sd = map->sdata; sd; sd = sd->next) { + for (sd = map->sdata; sd; sd = sd_next) { + sd_next = sd->next; ucimap_free_section(map, sd); } } @@ -201,11 +192,23 @@ ucimap_find_section(struct uci_map *map, struct ucimap_fixup *f) return NULL; } +static union ucimap_data * +ucimap_list_append(struct ucimap_list *list) +{ + if (unlikely(list->size <= list->n_items)) { + /* should not happen */ + DPRINTF("ERROR: overflow while filling a list (size=%d)\n", list->size); + return NULL; + } + return &list->item[list->n_items++]; +} + + static bool ucimap_handle_fixup(struct uci_map *map, struct ucimap_fixup *f) { void *ptr = ucimap_find_section(map, f); - struct ucimap_list *list; + union ucimap_data *data; if (!ptr) return false; @@ -215,8 +218,11 @@ ucimap_handle_fixup(struct uci_map *map, struct ucimap_fixup *f) f->data->ptr = ptr; break; case UCIMAP_LIST: - list = f->data->list; - list->item[list->n_items++].ptr = ptr; + data = ucimap_list_append(f->data->list); + if (!data) + return false; + + data->ptr = ptr; break; } return true; @@ -269,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; @@ -287,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; @@ -303,6 +314,7 @@ ucimap_add_fixup(struct ucimap_section_data *sd, union ucimap_data *data, struct struct ucimap_fixup *f, tmp; struct uci_map *map = sd->map; + tmp.next = NULL; tmp.sm = om->data.sm; tmp.name = str; tmp.type = om->type; @@ -340,13 +352,9 @@ ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_s int val; if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type)) { - if (unlikely(data->list->size <= data->list->n_items)) { - /* should not happen */ - DPRINTF("ERROR: overflow while filling a list\n"); + data = ucimap_list_append(data->list); + if (!data) return; - } - - data = &data->list->item[data->list->n_items++]; } switch(om->type & UCIMAP_SUBTYPE) { @@ -391,11 +399,10 @@ ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_s ucimap_add_fixup(sd, data, om, str); return; case UCIMAP_CUSTOM: - tdata.s = (char *) data; break; } if (om->parse) { - if (om->parse(ucimap_section_ptr(sd), om, &tdata, str) < 0) + if (om->parse(ucimap_section_ptr(sd), om, data, str) < 0) return; if (ucimap_is_custom(om->type) && om->free) { if (tdata.ptr != data->ptr) @@ -602,6 +609,7 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim union ucimap_data *data; struct uci_element *e; int n_elements = 0; + int n_elements_alloc = 0; int n_elements_custom = 0; int size; @@ -615,7 +623,8 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim if (o->type == UCI_TYPE_LIST) { uci_foreach_element(&o->v.list, tmp) { - ucimap_count_alloc(om, &n_elements, &n_elements_custom); + ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom); + n_elements++; } } else if ((o->type == UCI_TYPE_STRING) && ucimap_is_list_auto(om->type)) { @@ -628,7 +637,7 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim break; n_elements++; - ucimap_count_alloc(om, &n_elements, &n_elements_custom); + ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom); while (*data && !isspace(*data)) data++; @@ -641,7 +650,7 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim break; } /* add one more for the ucimap_list */ - n_alloc += n_elements + 1; + n_alloc += n_elements_alloc + 1; n_alloc_custom += n_elements_custom; size = sizeof(struct ucimap_list) + n_elements * sizeof(union ucimap_data); @@ -650,8 +659,8 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim if (!data->list) goto error_mem; - data->list->size = n_elements; memset(data->list, 0, size); + data->list->size = n_elements; } else { ucimap_count_alloc(om, &n_alloc, &n_alloc_custom); } @@ -704,8 +713,8 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim return 0; error_mem: - if (sd->allocmap) - free(sd->allocmap); + free(sd->alloc_custom); + free(sd->allocmap); free(sd); return UCI_ERR_MEM; @@ -780,13 +789,6 @@ ucimap_data_to_string(struct ucimap_section_data *sd, struct uci_optmap *om, uni } if (om->format) { - union ucimap_data tdata; - - if (ucimap_is_custom(om->type)) { - tdata.s = (char *)data; - data = &tdata; - } - if (om->format(ucimap_section_ptr(sd), om, data, &str) < 0) return NULL; @@ -887,6 +889,7 @@ ucimap_parse(struct uci_map *map, struct uci_package *pkg) } else { sd = malloc(sm->alloc_len); memset(sd, 0, sm->alloc_len); + sd = ucimap_ptr_section(sm, sd); } if (!sd) continue;