From c6bbc28da0a63f77c8a649ccb501da826dbf8aef Mon Sep 17 00:00:00 2001 From: Blake Taylor Date: Thu, 26 Jan 2023 20:13:49 -0800 Subject: [PATCH] adding check for ptr cookie to be the same as segment cookie to catch valid pointers when unaligned os memory is provided at allocation --- src/segment-cache.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/segment-cache.c b/src/segment-cache.c index d93fd6441..761dc7536 100644 --- a/src/segment-cache.c +++ b/src/segment-cache.c @@ -385,7 +385,15 @@ static bool mi_is_valid_pointer(const void* p) { } mi_decl_nodiscard mi_decl_export bool mi_is_in_heap_region(const void* p) mi_attr_noexcept { - return mi_is_valid_pointer(p); + if mi_likely(mi_is_valid_pointer(p)) { + return true; + } + + // when unable to allocate aligned OS memory directly, pointer cookie is same as segment cookie + mi_segment_t* const segment = _mi_ptr_segment(p); + if (segment == NULL) return false; + + return (_mi_ptr_cookie(segment) == segment->cookie); } /*