From ab089716f1dd37c71a293ee9f30a7541ecec95fb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marc=20Nieper-Wi=C3=9Fkirchen?= Date: Fri, 12 Aug 2022 16:00:23 +0200 Subject: [PATCH] hamt: fix technically undefined behavior Bug reported by Bruno Haible in . * lib/hamt.c (entry_insert): Remove technically undefined behavior when shifting an integer of N bits by N or more bits. --- ChangeLog | 8 ++++++++ lib/hamt.c | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/ChangeLog b/ChangeLog index d3fa2e0ddc..8aa95ecfba 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2022-08-12 Marc Nieper-Wißkirchen + + hamt: fix technically undefined behavior + Bug reported by Bruno Haible in + . + * lib/hamt.c (entry_insert): Remove technically undefined + behavior when shifting an integer of N bits by N or more bits. + 2022-08-09 Paul Eggert verify: port ‘assume’ to C23 non-GCC diff --git a/lib/hamt.c b/lib/hamt.c index 8a12f216d1..7600433806 100644 --- a/lib/hamt.c +++ b/lib/hamt.c @@ -680,6 +680,11 @@ entry_insert (const struct function_table *functions, Hamt_entry *entry, Hamt_entry *new_entry = copy_entry (*elt_ptr); if (replace) *elt_ptr = NULL; + /* We have to take this shortcut as shifting an integer of N + bits by N or more bits triggers undefined behavior. + See: https://lists.gnu.org/archive/html/bug-gnulib/2022-04/msg00023.html. */ + if (depth >= _GL_HAMT_MAX_DEPTH) + return (Hamt_entry *) create_populated_bucket (new_entry, copy_entry (entry)); return create_populated_subtrie (new_entry, copy_entry (entry), hash, (hash_element (functions, entry) >> (5 * depth)), depth); -- 2.39.5