[PATCH] D33588: Fix two sources of UB in __next_hash_pow2 (from __hash_table)

Vedant Kumar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 1 10:27:09 PDT 2017


vsk added inline comments.


================
Comment at: include/__hash_table:139
 {
-    return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
+    return (__n > 1) ? (size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1))) : __n;
 }
----------------
EricWF wrote:
> Shouldn't this return  `__n + 1` when `__n <= 1`, or even 2 in both cases?
I thought "next_hash_pow2(n)" meant "a hash based on n or the first power-of-two GTE n", but I suppose it's actually "first power-of-two GTE than n, for hash tables". In this case, returning `1` when `__n <= 1` makes the most sense to me, since it's the first power of two GTE 0 and 1. What do you think?


https://reviews.llvm.org/D33588





More information about the cfe-commits mailing list