From 4ff474fdcaf82bc462942d8681884450e3c8d389 Mon Sep 17 00:00:00 2001 From: Lukasz Dorau Date: Fri, 6 Dec 2024 17:11:14 +0100 Subject: [PATCH 1/2] Fix: remove incorrect assert in utils_align_ptr_up_size_down() Remove incorrect assert in utils_align_ptr_up_size_down(). A pointer is aligned, but a size is only adjusted to the new pointer. The size does not have to be aligned. Signed-off-by: Lukasz Dorau --- src/utils/utils_common.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/utils_common.c b/src/utils/utils_common.c index bffc9f3559..eaf5420fc6 100644 --- a/src/utils/utils_common.c +++ b/src/utils/utils_common.c @@ -25,7 +25,6 @@ void utils_align_ptr_up_size_down(void **ptr, size_t *size, size_t alignment) { } ASSERT(IS_ALIGNED(p, alignment)); - ASSERT(IS_ALIGNED(s, alignment)); *ptr = (void *)p; *size = s; From dd47ffc2b8ec3eeb68ff4bf6789710aee50e76fe Mon Sep 17 00:00:00 2001 From: Lukasz Dorau Date: Mon, 9 Dec 2024 15:53:02 +0100 Subject: [PATCH 2/2] Add tests for utils_align_ptr_up_size_down() Signed-off-by: Lukasz Dorau --- test/utils/utils_linux.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/utils/utils_linux.cpp b/test/utils/utils_linux.cpp index 7aa0a9d834..1815a1a782 100644 --- a/test/utils/utils_linux.cpp +++ b/test/utils/utils_linux.cpp @@ -2,6 +2,7 @@ // Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include #include #include "base.hpp" @@ -169,3 +170,19 @@ TEST_F(test, utils_open) { EXPECT_EQ(utils_file_open(NULL), -1); EXPECT_EQ(utils_file_open_or_create(NULL), -1); } + +TEST_F(test, utils_align_ptr_up_size_down) { + uintptr_t ptr = 0x4000; + size_t size = 0x8000; + size_t alignment = 0x4000; + utils_align_ptr_up_size_down((void **)&ptr, &size, alignment); + EXPECT_EQ(ptr, 0x4000); + EXPECT_EQ(size, 0x8000); + + ptr = 0x4001; + size = 0x8000; + alignment = 0x4000; + utils_align_ptr_up_size_down((void **)&ptr, &size, alignment); + EXPECT_EQ(ptr, 0x8000); + EXPECT_EQ(size, 0x4001); +}