add sanity checks for optmap section type vs sectionmap type
authorFelix Fietkau <nbd@openwrt.org>
Fri, 4 Sep 2009 18:07:06 +0000 (20:07 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Fri, 4 Sep 2009 18:07:25 +0000 (20:07 +0200)
ucimap.c
ucimap.h

index 6d97b070c7c2a986d3237c20e4f06c0988142ab8..9aafe0b1cc8cca5f5c16fc9e7953d71c39284b1d 100644 (file)
--- a/ucimap.c
+++ b/ucimap.c
@@ -439,6 +439,14 @@ ucimap_check_optmap_type(struct uci_sectionmap *sm, struct uci_optmap *om)
 {
        unsigned int type;
 
+       if (unlikely(sm->type_name != om->type_name) &&
+           unlikely(strcmp(sm->type_name, om->type_name) != 0)) {
+               DPRINTF("Option '%s' of section type '%s' refereces unknown "
+                       "section type '%s', should be '%s'.\n",
+                       om->name, sm->type, om->type_name, sm->type_name);
+               return false;
+       }
+
        if (om->detected_type < 0)
                return true;
 
index 2195e78bb3fccc1119881d34c152d474c0ba7a34..d0e8ce91c5f56611be8d5db1e67754b478b0d766 100644 (file)
--- a/ucimap.h
+++ b/ucimap.h
        (_name[(_bit) / 8] & (1 << ((_bit) % 8)))
 
 #ifndef __GNUC__
+
 #define __optmap_gen_type(_type, _field) -1
-#else
+
+#ifndef likely
+#define likely(_expr) !!(_expr)
+#endif
+
+#ifndef unlikely
+#define unlikely(_expr) !!(_expr)
+#endif
+
+#else /* __GNUC__ */
 
 #define __compatible(_type, _field, _newtype) \
        __builtin_types_compatible_p(typeof(&(((_type *)0)->_field)), _newtype *)
        __bool_compatible(_type, _field, UCIMAP_BOOL, \
        -1))))
 
+#ifndef likely
+#define likely(x)   __builtin_expect(!!(x), 1)
 #endif
 
+#ifndef unlikely
+#define unlikely(x) __builtin_expect(!!(x), 0)
+#endif
+
+#endif /* __GNUC__ */
+
 #define UCIMAP_OPTION(_type, _field) \
        .type = UCIMAP_CUSTOM, \
        .name = #_field, \
        .offset = offsetof(_type, _field), \
-       .detected_type = __optmap_gen_type(_type, _field)
+       .detected_type = __optmap_gen_type(_type, _field), \
+       .type_name = #_type
 
 
 #define UCIMAP_SECTION(_name, _field) \
        .alloc_len = sizeof(_name), \
-       .smap_offset = offsetof(_name, _field)
+       .smap_offset = offsetof(_name, _field), \
+       .type_name = #_name
 
 struct uci_sectionmap;
 struct uci_optmap;
@@ -177,13 +197,15 @@ struct uci_sectionmap {
        struct uci_optmap *options;
        unsigned int n_options;
        unsigned int options_size;
+
+       /* internal */
+       const char *type_name;
 };
 
 struct uci_optmap {
        unsigned int offset;
        const char *name;
        enum ucimap_type type;
-       int detected_type;
        int (*parse)(void *section, struct uci_optmap *om, union ucimap_data *data, const char *string);
        int (*format)(void *section, struct uci_optmap *om, union ucimap_data *data, char **string);
        void (*free)(void *section, struct uci_optmap *om, void *ptr);
@@ -198,6 +220,10 @@ struct uci_optmap {
                } s;
                struct uci_sectionmap *sm;
        } data;
+
+       /* internal */
+       int detected_type;
+       const char *type_name;
 };
 
 struct ucimap_list {