]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib: make qobj NULL-safe/aware
authorDavid Lamparter <equinox@opensourcerouting.org>
Wed, 7 Dec 2016 11:16:05 +0000 (12:16 +0100)
committerDavid Lamparter <equinox@opensourcerouting.org>
Fri, 9 Dec 2016 16:36:25 +0000 (17:36 +0100)
reserve qobj ID 0 for a NULL pointer.  (No change is needed for lookups
since looking up 0 will simply fail and return NULL.)

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
lib/qobj.c
lib/qobj.h
lib/vty.h

index f64972e32aa3569ec6795cd96af6338d2019ba06..aeae52e0297467dfd456e2561f091fdcddbe9ad3 100644 (file)
@@ -49,7 +49,7 @@ void qobj_reg(struct qobj_node *node, struct qobj_nodetype *type)
       node->nid  = (uint64_t)random();
       node->nid ^= (uint64_t)random() << 32;
     }
-  while (hash_get (nodes, node, hash_alloc_intern) != node);
+  while (!node->nid || hash_get (nodes, node, hash_alloc_intern) != node);
 }
 
 void qobj_unreg(struct qobj_node *node)
index 4c326731ed11df5311e6c0bf4bd341cf7eb97531..64a6774bff73897de812493463161bd4c842c974 100644 (file)
@@ -109,6 +109,8 @@ void *qobj_get_typed(uint64_t id, struct qobj_nodetype *type);
 
 #define QOBJ_ID(ptr) \
        ((ptr)->qobj_node.nid)
+#define QOBJ_ID_0SAFE(ptr) \
+       ({ typeof (ptr) _ptr = (ptr); _ptr ? _ptr->qobj_node.nid : 0ULL; })
 
 void qobj_init(void);
 void qobj_finish(void);
index 51d61b4a8cae944e91ed8e32d6f812159c797e82..3ca9159211e9c7eb4ae11748166b1f31206730e7 100644 (file)
--- a/lib/vty.h
+++ b/lib/vty.h
@@ -157,14 +157,18 @@ static inline void vty_push_context(struct vty *vty,
 #endif
 }
 
+/* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
+ * dereference "NULL->qobj_node.nid" */
 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
-       vty_push_context(vty, nodeval, QOBJ_ID(ptr), NULL)
+       vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr), NULL)
+#define VTY_PUSH_CONTEXT_NULL(nodeval) \
+       vty_push_context(vty, nodeval, 0ULL, NULL)
 #define VTY_PUSH_CONTEXT_COMPAT(nodeval, ptr) \
-       vty_push_context(vty, nodeval, QOBJ_ID(ptr), ptr)
+       vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr), ptr)
 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) do { \
                vty->node = nodeval; \
                /* qobj_index stays untouched */ \
-               vty->qobj_index_sub = QOBJ_ID(ptr); \
+               vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
        } while (0)
 
 /* can return NULL if context is invalid! */