]> git.kaiwu.me - njs.git/commitdiff
Memory pool: compare block addresses without overflow
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 17 Sep 2026 23:47:35 +0000 (16:47 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Fri, 18 Sep 2026 20:42:16 +0000 (13:42 -0700)
Previously, the rbtree comparator converted the difference between two
addresses to intptr_t.  On 32-bit systems, addresses that differ by at
least 2 GiB could produce an incorrect ordering and prevent a later block
lookup from finding its cluster.

The fix is to compare the addresses directly and return -1, 0, or 1.
This fixes a pool that remained non-empty after all allocations were freed
in the x86 memory pool test.

src/njs_mp.c

index f7e0df1d1110d1e7e3ec0bbfb8b2ac5026ffd4c2..f1d464a326a535cd293af6e7bce71c1e1a59f124 100644 (file)
@@ -647,12 +647,16 @@ njs_mp_alloc_large(njs_mp_t *mp, size_t alignment, size_t size)
 static intptr_t
 njs_mp_rbtree_compare(njs_rbtree_node_t *node1, njs_rbtree_node_t *node2)
 {
+    uintptr_t        start1, start2;
     njs_mp_block_t  *block1, *block2;
 
     block1 = (njs_mp_block_t *) node1;
     block2 = (njs_mp_block_t *) node2;
 
-    return (uintptr_t) block1->start - (uintptr_t) block2->start;
+    start1 = (uintptr_t) block1->start;
+    start2 = (uintptr_t) block2->start;
+
+    return (start1 > start2) - (start1 < start2);
 }