From: David Lamparter Date: Wed, 7 Dec 2016 11:16:05 +0000 (+0100) Subject: lib: make qobj NULL-safe/aware X-Git-Tag: frr-3.0-branchpoint~114^2~4 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=3c5070bec0a64918ae87b0992221b8e6c98ff136;p=mirror%2Ffrr.git lib: make qobj NULL-safe/aware 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 --- diff --git a/lib/qobj.c b/lib/qobj.c index f64972e32a..aeae52e029 100644 --- a/lib/qobj.c +++ b/lib/qobj.c @@ -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) diff --git a/lib/qobj.h b/lib/qobj.h index 4c326731ed..64a6774bff 100644 --- a/lib/qobj.h +++ b/lib/qobj.h @@ -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); diff --git a/lib/vty.h b/lib/vty.h index 51d61b4a8c..3ca9159211 100644 --- 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! */