remove internal usage of redundant uci_ptr.last
[project/uci.git] / ucimap.c
index 6a5c11721bbc79e192d22785e8ce1c7997bdc342..758a5eabda266149e08a52c5f85872156e65b91c 100644 (file)
--- a/ucimap.c
+++ b/ucimap.c
@@ -13,7 +13,7 @@
  */
 
 /*
- * 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 <strings.h>
@@ -134,7 +134,7 @@ void
 ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd)
 {
        void *section;
-       int i;
+       unsigned int i;
 
        section = ucimap_section_ptr(sd);
        if (sd->ref)
@@ -234,7 +234,7 @@ ucimap_free_item(struct ucimap_section_data *sd, void *item)
        struct ucimap_alloc_custom *ac;
        struct ucimap_alloc *a;
        void *ptr = *((void **) item);
-       int i;
+       unsigned int i;
 
        if (!ptr)
                return;
@@ -270,7 +270,8 @@ ucimap_resize_list(struct ucimap_section_data *sd, struct ucimap_list **list, in
 {
        struct ucimap_list *new;
        struct ucimap_alloc *a;
-       int i, offset = 0;
+       unsigned int i;
+       int offset = 0;
        int size = sizeof(struct ucimap_list) + items * sizeof(union ucimap_data);
 
        if (!*list) {
@@ -295,6 +296,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;
@@ -311,6 +315,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;
@@ -356,7 +361,7 @@ ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_s
        switch(om->type & UCIMAP_SUBTYPE) {
        case UCIMAP_STRING:
                if ((om->data.s.maxlen > 0) &&
-                       (strlen(str) > om->data.s.maxlen))
+                       (strlen(str) > (unsigned) om->data.s.maxlen))
                        return;
 
                s = strdup(str);
@@ -485,16 +490,21 @@ ucimap_add_section_list(struct uci_map *map, struct ucimap_section_data *sd)
        map->sdata_tail = &sd->next;
 }
 
-static void
+static int
 ucimap_add_section(struct ucimap_section_data *sd)
 {
+       int r;
        struct uci_map *map = sd->map;
 
        sd->next = NULL;
-       if (sd->sm->add(map, ucimap_section_ptr(sd)) < 0)
+       r = sd->sm->add(map, ucimap_section_ptr(sd));
+       if (r < 0) {
                ucimap_free_section(map, sd);
-       else
+               return r;
+       } else
                ucimap_add_section_list(map, sd);
+
+       return 0;
 }
 
 #ifdef UCI_DEBUG
@@ -528,7 +538,7 @@ ucimap_get_type_name(int type)
 static bool
 ucimap_check_optmap_type(struct uci_sectionmap *sm, struct uci_optmap *om)
 {
-       unsigned int type;
+       int type;
 
        if (unlikely(sm->type_name != om->type_name) &&
            unlikely(strcmp(sm->type_name, om->type_name) != 0)) {
@@ -651,11 +661,10 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim
                        size = sizeof(struct ucimap_list) +
                                n_elements * sizeof(union ucimap_data);
 
-                       data->list = malloc(size);
+                       data->list = calloc(1, size);
                        if (!data->list)
                                goto error_mem;
 
-                       memset(data->list, 0, size);
                        data->list->size = n_elements;
                } else {
                        ucimap_count_alloc(om, &n_alloc, &n_alloc_custom);
@@ -697,7 +706,9 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim
                goto error;
 
        if (map->parsed) {
-               ucimap_add_section(sd);
+               err = ucimap_add_section(sd);
+               if (err)
+                       return err;
        } else {
                ucimap_add_section_list(map, sd);
        }
@@ -709,8 +720,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;
 
@@ -742,7 +753,7 @@ ucimap_set_changed(struct ucimap_section_data *sd, void *field)
        void *section = ucimap_section_ptr(sd);
        struct uci_sectionmap *sm = sd->sm;
        struct uci_optmap *om;
-       int ofs = (char *)field - (char *)section;
+       unsigned int ofs = (char *)field - (char *)section;
        int i = 0;
 
        ucimap_foreach_option(sm, om) {
@@ -864,7 +875,7 @@ ucimap_parse(struct uci_map *map, struct uci_package *pkg)
        struct uci_element *e;
        struct ucimap_section_data *sd, **sd_tail;
        struct ucimap_fixup *f;
-       int i;
+       unsigned int i;
 
        sd_tail = map->sdata_tail;
        map->parsed = false;
@@ -881,14 +892,15 @@ ucimap_parse(struct uci_map *map, struct uci_package *pkg)
 
                        if (sm->alloc) {
                                sd = sm->alloc(map, sm, s);
+                               if (!sd)
+                                       continue;
                                memset(sd, 0, sizeof(struct ucimap_section_data));
                        } else {
-                               sd = malloc(sm->alloc_len);
-                               memset(sd, 0, sm->alloc_len);
+                               sd = calloc(1, sm->alloc_len);
+                               if (!sd)
+                                       continue;
                                sd = ucimap_ptr_section(sm, sd);
                        }
-                       if (!sd)
-                               continue;
 
                        ucimap_parse_section(map, sm, sd, s);
                }