]> Savannah Git Hosting - gnulib.git/commitdiff
hamt: fix technically undefined behavior
authorMarc Nieper-Wißkirchen <marc@nieper-wisskirchen.de>
Fri, 12 Aug 2022 14:00:23 +0000 (16:00 +0200)
committerMarc Nieper-Wißkirchen <marc@nieper-wisskirchen.de>
Fri, 12 Aug 2022 14:00:23 +0000 (16:00 +0200)
Bug reported by Bruno Haible in
<https://lists.gnu.org/r/bug-gnulib/2022-04/msg00023.html>.
* lib/hamt.c (entry_insert): Remove technically undefined
behavior when shifting an integer of N bits by N or more bits.

ChangeLog
lib/hamt.c

index ccf4acd03d0799b3670cdcef3a27eebb35bfdfad..0d04cb70e70aaa8d9a9af1299b6df0152254f45f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2022-08-12  Marc Nieper-Wißkirchen  <marc@nieper-wisskirchen.de>
+
+       hamt: fix technically undefined behavior
+       Bug reported by Bruno Haible in
+       <https://lists.gnu.org/r/bug-gnulib/2022-04/msg00023.html>.
+       * lib/hamt.c (entry_insert): Remove technically undefined
+       behavior when shifting an integer of N bits by N or more bits.
+
 2022-08-10  Paul Eggert  <eggert@cs.ucla.edu>
 
        stdckdint: fix dependency
index 2b07cf23b81a69d75396cf2434e1d939fb4c895c..be9712561a39c2effe1eb1266cee732ef6944d80 100644 (file)
@@ -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);