From 6ecac2d0bcc36a3b5273b7b471e2913c6816a264 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 10 Feb 2025 08:26:09 -0600 Subject: [PATCH] Use "uintptr_t" for 64-bit targets On 64-bit targets, casting from pointer to unsigned can lose data. Instead, cast to uintptr_t. This causes no code change on 32-bit platforms, since the types have the same width, but preserves all bits on systems where pointers are 64 bits. --- tlsf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tlsf.c b/tlsf.c index aa36db4..419fdf2 100644 --- a/tlsf.c +++ b/tlsf.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "tlsf.h" #include "tlsf_common.h" #include "tlsf_block_functions.h" @@ -1036,11 +1037,11 @@ void* tlsf_malloc_addr(tlsf_t tlsf, size_t size, void *address) control_t* control = tlsf_cast(control_t*, tlsf); /* adjust the address to be ALIGN_SIZE bytes aligned. */ - const unsigned int addr_adjusted = align_down(tlsf_cast(unsigned int, address), ALIGN_SIZE); + const uintptr_t addr_adjusted = align_down(tlsf_cast(uintptr_t, address), ALIGN_SIZE); /* adjust the size to be ALIGN_SIZE bytes aligned. Add to the size the difference * between the requested address and the address_adjusted. */ - size_t size_adjusted = align_up(size + (tlsf_cast(unsigned int, address) - addr_adjusted), ALIGN_SIZE); + size_t size_adjusted = align_up(size + (tlsf_cast(uintptr_t, address) - addr_adjusted), ALIGN_SIZE); /* find the free block that starts before the address in the pool and is big enough * to support the size of allocation at the given address */ @@ -1080,7 +1081,7 @@ void* tlsf_malloc_addr(tlsf_t tlsf, size_t size, void *address) /* trim any leading space or add the leading space to the overall requested size * if the leading space is not big enough to store a block of minimum size */ - const size_t space_before_addr_adjusted = addr_adjusted - tlsf_cast(unsigned int, block_to_ptr(block)); + const size_t space_before_addr_adjusted = addr_adjusted - tlsf_cast(uintptr_t, block_to_ptr(block)); block_header_t *return_block = block; if (space_before_addr_adjusted >= block_size_min) { return_block = block_trim_free_leading(control, block, space_before_addr_adjusted);