ucimap: fix boolean interpretation, add range checks for int values (thx, henning)
[project/uci.git] / ucimap.c
index 8fdeed2..7dba62a 100644 (file)
--- a/ucimap.c
+++ b/ucimap.c
@@ -16,6 +16,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <limits.h>
 #include "ucimap.h"
 
 struct uci_alloc {
@@ -27,28 +28,12 @@ struct uci_alloc {
 
 struct uci_fixup {
        struct list_head list;
-       struct uci_sectmap *sm;
+       struct uci_sectionmap *sm;
        const char *name;
        enum ucimap_type type;
        union ucimap_data *data;
 };
 
-struct uci_sectmap_data {
-       struct list_head list;
-       struct uci_map *map;
-       struct uci_sectmap *sm;
-       const char *section_name;
-
-       /* list of allocations done by ucimap */
-       struct uci_alloc *allocmap;
-       unsigned long allocmap_len;
-
-       /* map for changed fields */
-       unsigned char *cmap;
-       bool done;
-};
-
-
 #define ucimap_foreach_option(_sm, _o) \
        if (!(_sm)->options_size) \
                (_sm)->options_size = sizeof(struct uci_optmap); \
@@ -100,14 +85,13 @@ ucimap_is_custom(enum ucimap_type type)
 }
 
 static inline void *
-ucimap_section_ptr(struct uci_sectmap_data *sd)
+ucimap_section_ptr(struct ucimap_section_data *sd)
 {
-       void *data = sd + 1;
-       return data;
+       return ((char *) sd - sd->sm->smap_offset);
 }
 
 static inline union ucimap_data *
-ucimap_get_data(struct uci_sectmap_data *sd, struct uci_optmap *om)
+ucimap_get_data(struct ucimap_section_data *sd, struct uci_optmap *om)
 {
        void *data;
 
@@ -135,7 +119,7 @@ ucimap_free_item(struct uci_alloc *a)
 }
 
 static void
-ucimap_add_alloc(struct uci_sectmap_data *sd, void *ptr)
+ucimap_add_alloc(struct ucimap_section_data *sd, void *ptr)
 {
        struct uci_alloc *a = &sd->allocmap[sd->allocmap_len++];
        a->type = UCIMAP_SIMPLE;
@@ -143,7 +127,7 @@ ucimap_add_alloc(struct uci_sectmap_data *sd, void *ptr)
 }
 
 static void
-ucimap_free_section(struct uci_map *map, struct uci_sectmap_data *sd)
+ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd)
 {
        void *section;
        int i;
@@ -169,7 +153,7 @@ ucimap_cleanup(struct uci_map *map)
        struct list_head *ptr, *tmp;
 
        list_for_each_safe(ptr, tmp, &map->sdata) {
-               struct uci_sectmap_data *sd = list_entry(ptr, struct uci_sectmap_data, list);
+               struct ucimap_section_data *sd = list_entry(ptr, struct ucimap_section_data, list);
                ucimap_free_section(map, sd);
        }
 }
@@ -192,10 +176,11 @@ ucimap_add_fixup(struct uci_map *map, union ucimap_data *data, struct uci_optmap
 }
 
 static void
-ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct uci_sectmap_data *sd, const char *str)
+ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
 {
        union ucimap_data tdata = *data;
        char *eptr = NULL;
+       long lval;
        char *s;
        int val;
 
@@ -213,28 +198,30 @@ ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct uci_sect
                ucimap_add_alloc(sd, s);
                break;
        case UCIMAP_BOOL:
-               val = -1;
-               if (strcmp(str, "on"))
+               if (!strcmp(str, "on"))
                        val = true;
-               else if (strcmp(str, "1"))
+               else if (!strcmp(str, "1"))
                        val = true;
-               else if (strcmp(str, "enabled"))
+               else if (!strcmp(str, "enabled"))
                        val = true;
-               else if (strcmp(str, "off"))
+               else if (!strcmp(str, "off"))
                        val = false;
-               else if (strcmp(str, "0"))
+               else if (!strcmp(str, "0"))
                        val = false;
-               else if (strcmp(str, "disabled"))
+               else if (!strcmp(str, "disabled"))
                        val = false;
-               if (val == -1)
+               else
                        return;
 
                tdata.b = val;
                break;
        case UCIMAP_INT:
-               val = strtol(str, &eptr, om->data.i.base);
+               lval = strtol(str, &eptr, om->data.i.base);
+               if (lval < INT_MIN || lval > INT_MAX)
+                       return;
+
                if (!eptr || *eptr == '\0')
-                       tdata.i = val;
+                       tdata.i = (int) lval;
                else
                        return;
                break;
@@ -256,7 +243,7 @@ ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct uci_sect
 
 
 static int
-ucimap_parse_options(struct uci_map *map, struct uci_sectmap *sm, struct uci_sectmap_data *sd, struct uci_section *s)
+ucimap_parse_options(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
 {
        struct uci_element *e, *l;
        struct uci_option *o;
@@ -290,21 +277,29 @@ ucimap_parse_options(struct uci_map *map, struct uci_sectmap *sm, struct uci_sec
 
 
 static int
-ucimap_parse_section(struct uci_map *map, struct uci_sectmap *sm, struct uci_section *s)
+ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s)
 {
-       struct uci_sectmap_data *sd = NULL;
+       struct ucimap_section_data *sd = NULL;
        struct uci_optmap *om;
        char *section_name;
        void *section;
        int n_alloc = 2;
        int err;
 
-       sd = malloc(sm->alloc_len + sizeof(struct uci_sectmap_data));
+       if (sm->alloc) {
+               sd = sm->alloc(map, sm, s);
+               memset(sd, 0, sizeof(struct ucimap_section_data));
+       } else {
+               sd = malloc(sm->alloc_len);
+               memset(sd, 0, sm->alloc_len);
+       }
+
        if (!sd)
                return UCI_ERR_MEM;
 
-       memset(sd, 0, sm->alloc_len + sizeof(struct uci_sectmap_data));
        INIT_LIST_HEAD(&sd->list);
+       sd->map = map;
+       sd->sm = sm;
 
        ucimap_foreach_option(sm, om) {
                if (ucimap_is_list(om->type)) {
@@ -336,8 +331,6 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectmap *sm, struct uci_sec
                }
        }
 
-       sd->map = map;
-       sd->sm = sm;
        sd->allocmap = malloc(n_alloc * sizeof(struct uci_alloc));
        if (!sd->allocmap)
                goto error_mem;
@@ -363,7 +356,6 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectmap *sm, struct uci_sec
        }
 
        section = ucimap_section_ptr(sd);
-
        err = sm->init(map, section, s);
        if (err)
                goto error;
@@ -404,11 +396,10 @@ ucimap_fill_ptr(struct uci_ptr *ptr, struct uci_section *s, const char *option)
 }
 
 void
-ucimap_set_changed(void *section, void *field)
+ucimap_set_changed(struct ucimap_section_data *sd, void *field)
 {
-       char *sptr = (char *)section - sizeof(struct uci_sectmap_data);
-       struct uci_sectmap_data *sd = (struct uci_sectmap_data *) sptr;
-       struct uci_sectmap *sm = sd->sm;
+       void *section = ucimap_section_ptr(sd);
+       struct uci_sectionmap *sm = sd->sm;
        struct uci_optmap *om;
        int ofs = (char *)field - (char *)section;
        int i = 0;
@@ -423,11 +414,9 @@ ucimap_set_changed(void *section, void *field)
 }
 
 int
-ucimap_store_section(struct uci_map *map, struct uci_package *p, void *section)
+ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd)
 {
-       char *sptr = (char *)section - sizeof(struct uci_sectmap_data);
-       struct uci_sectmap_data *sd = (struct uci_sectmap_data *) sptr;
-       struct uci_sectmap *sm = sd->sm;
+       struct uci_sectionmap *sm = sd->sm;
        struct uci_section *s = NULL;
        struct uci_optmap *om;
        struct uci_element *e;
@@ -447,13 +436,14 @@ ucimap_store_section(struct uci_map *map, struct uci_package *p, void *section)
        ucimap_foreach_option(sm, om) {
                union ucimap_data *data;
                static char buf[32];
-               const char *str = NULL;
+               char *str = NULL;
 
+               i++;
                if (ucimap_is_list(om->type))
                        continue;
 
                data = ucimap_get_data(sd, om);
-               if (!TEST_BIT(sd->cmap, i))
+               if (!TEST_BIT(sd->cmap, i - 1))
                        continue;
 
                ucimap_fill_ptr(&ptr, s, om->name);
@@ -469,17 +459,32 @@ ucimap_store_section(struct uci_map *map, struct uci_package *p, void *section)
                        sprintf(buf, "%d", !!data->b);
                        str = buf;
                        break;
+               case UCIMAP_CUSTOM:
+                       break;
                default:
                        continue;
                }
+               if (om->format) {
+                       union ucimap_data tdata, *data;
+
+                       data = ucimap_get_data(sd, om);
+                       if (ucimap_is_custom(om->type)) {
+                               tdata.s = (char *)data;
+                               data = &tdata;
+                       }
+
+                       if (om->format(ucimap_section_ptr(sd), om, data, &str) < 0)
+                               continue;
+               }
+               if (!str)
+                       continue;
                ptr.value = str;
 
                ret = uci_set(s->package->ctx, &ptr);
                if (ret)
                        return ret;
 
-               CLR_BIT(sd->cmap, i);
-               i++;
+               CLR_BIT(sd->cmap, i - 1);
        }
 
        return 0;
@@ -488,18 +493,16 @@ ucimap_store_section(struct uci_map *map, struct uci_package *p, void *section)
 void *
 ucimap_find_section(struct uci_map *map, struct uci_fixup *f)
 {
-       struct uci_sectmap_data *sd;
+       struct ucimap_section_data *sd;
        struct list_head *p;
-       void *ret;
 
        list_for_each(p, &map->sdata) {
-               sd = list_entry(p, struct uci_sectmap_data, list);
+               sd = list_entry(p, struct ucimap_section_data, list);
                if (sd->sm != f->sm)
                        continue;
                if (strcmp(f->name, sd->section_name) != 0)
                        continue;
-               ret = (char *)sd + sizeof(struct uci_sectmap_data);
-               return ret;
+               return ucimap_section_ptr(sd);
        }
        return NULL;
 }
@@ -541,13 +544,13 @@ ucimap_parse(struct uci_map *map, struct uci_package *pkg)
                free(f);
        }
        list_for_each_safe(p, tmp, &map->sdata) {
-               struct uci_sectmap_data *sd = list_entry(p, struct uci_sectmap_data, list);
+               struct ucimap_section_data *sd = list_entry(p, struct ucimap_section_data, list);
                void *section;
 
                if (sd->done)
                        continue;
 
-               section = (char *) sd + sizeof(struct uci_sectmap_data);
+               section = ucimap_section_ptr(sd);
                if (sd->sm->add(map, section) != 0)
                        ucimap_free_section(map, sd);
        }