Skip to content

Commit 084d943

Browse files
authored
[flang] Fix excess allocation (#96663)
A recent patch introduced an error in an aligned byte size calculation that causes an extra word to be allocated when the original byte size is already aligned (including the case of zero). Fix.
1 parent d74fa1f commit 084d943

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

flang/runtime/pointer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void RTDEF(PointerAssociateRemapping)(Descriptor &pointer,
127127
RT_API_ATTRS void *AllocateValidatedPointerPayload(std::size_t byteSize) {
128128
// Add space for a footer to validate during deallocation.
129129
constexpr std::size_t align{sizeof(std::uintptr_t)};
130-
byteSize = ((byteSize / align) + 1) * align;
130+
byteSize = ((byteSize + align - 1) / align) * align;
131131
std::size_t total{byteSize + sizeof(std::uintptr_t)};
132132
void *p{std::malloc(total)};
133133
if (p) {
@@ -197,7 +197,7 @@ static RT_API_ATTRS std::size_t GetByteSize(
197197
bool RT_API_ATTRS ValidatePointerPayload(const ISO::CFI_cdesc_t &desc) {
198198
std::size_t byteSize{GetByteSize(desc)};
199199
constexpr std::size_t align{sizeof(std::uintptr_t)};
200-
byteSize = ((byteSize / align) + 1) * align;
200+
byteSize = ((byteSize + align - 1) / align) * align;
201201
const void *p{desc.base_addr};
202202
const std::uintptr_t *footer{reinterpret_cast<const std::uintptr_t *>(
203203
static_cast<const char *>(p) + byteSize)};

0 commit comments

Comments
 (0)