[ccan] [PATCH 5/5] idtree: Fix undefined behaviour (left shift of signed value)

David Gibson david at gibson.dropbear.id.au
Thu Jan 28 00:53:32 AEDT 2016


~0 will be signed and negative on any 2s complement system, and
left shifting a negative value has undefined behaviour in C.

Signed-off-by: David Gibson <david at gibson.dropbear.id.au>
---
 ccan/idtree/idtree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ccan/idtree/idtree.c b/ccan/idtree/idtree.c
index 5bd8882..6e75450 100644
--- a/ccan/idtree/idtree.c
+++ b/ccan/idtree/idtree.c
@@ -278,7 +278,7 @@ void *idtree_lookup(const struct idtree *idp, int id)
 	 * present.  If so, tain't one of ours!
 	 */
 	if (n + IDTREE_BITS < 31 &&
-	    (id & ~(~0 << MAX_ID_SHIFT)) >> (n + IDTREE_BITS))
+	    (id & ~(~0U << MAX_ID_SHIFT)) >> (n + IDTREE_BITS))
 	     return NULL;
 
 	/* Mask off upper bits we don't use for the search. */
-- 
2.5.0



More information about the ccan mailing list