[commited] malloc: Fix checking for small negative values of tcache_key

Samuel Thibault samuel.thibault@gnu.org
Fri Aug 29 22:33:05 GMT 2025


Andreas Schwab, le sam. 23 août 2025 15:37:11 +0200, a ecrit:
> On Aug 10 2025, Samuel Thibault wrote:
> 
> > +  while (labs ((intptr_t) tcache_key) <= 0x1000000
> 
> Can't this produce undefined behaviour?

Mmm, right, maybe not undefined behavior, but at least possibly not
doing what we actually wanted. This should be fine?

Samuel

diff --git a/malloc/malloc.c b/malloc/malloc.c
index e08873cad5..8970c0c274 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3108,7 +3108,8 @@ tcache_key_initialize (void)
   int minimum_bits = __WORDSIZE / 4;
   int maximum_bits = __WORDSIZE - minimum_bits;
 
-  while (labs ((intptr_t) tcache_key) <= 0x1000000
+  while (tcache_key <= 0x1000000
+      || tcache_key >= ((uintptr_t) ULONG_MAX) - 0x1000000
       || stdc_count_ones (tcache_key) < minimum_bits
       || stdc_count_ones (tcache_key) > maximum_bits)
     {


More information about the Libc-alpha mailing list