From e33365564bdd34f9c85a5f508dba8940437c74de Mon Sep 17 00:00:00 2001 From: Thibaut Sautereau Date: Wed, 10 Feb 2021 09:25:55 +0100 Subject: [PATCH] Fix wrong pointer being checked for NULL It's the region pointer that can be NULL here, and p was checked at the beginning of the function. Also fix the test accordingly. --- h_malloc.c | 2 +- test/simple-memory-corruption/test_smc.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/h_malloc.c b/h_malloc.c index 516c5e9..f2e75cb 100644 --- a/h_malloc.c +++ b/h_malloc.c @@ -1633,7 +1633,7 @@ EXPORT size_t h_malloc_usable_size(H_MALLOC_USABLE_SIZE_CONST void *p) { struct region_allocator *ra = ro.region_allocator; mutex_lock(&ra->lock); struct region_metadata *region = regions_find(p); - if (p == NULL) { + if (region == NULL) { fatal_error("invalid malloc_usable_size"); } size_t size = region->size; diff --git a/test/simple-memory-corruption/test_smc.py b/test/simple-memory-corruption/test_smc.py index 4122d4a..30e3269 100644 --- a/test/simple-memory-corruption/test_smc.py +++ b/test/simple-memory-corruption/test_smc.py @@ -145,9 +145,11 @@ class TestSimpleMemoryCorruption(unittest.TestCase): "fatal allocator error: invalid free\n") def test_uninitialized_malloc_usable_size(self): - _stdout, _stderr, returncode = self.run_test( + _stdout, stderr, returncode = self.run_test( "uninitialized_malloc_usable_size") - self.assertEqual(returncode, -11) + self.assertEqual(returncode, -6) + self.assertEqual(stderr.decode("utf-8"), + "fatal allocator error: invalid malloc_usable_size\n") def test_uninitialized_realloc(self): _stdout, stderr, returncode = self.run_test("uninitialized_realloc")