Skip to content

Commit 18726ad

Browse files
authored
YQL-19767: Realloc udf fix (#16721)
1 parent 517f14c commit 18726ad

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Diff for: ydb/library/yql/udfs/common/roaring/roaring.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,21 @@ namespace {
645645
return nullptr;
646646
}
647647

648+
// Get the old allocation information.
649+
auto oldAllocatedMemPointer = ((void**)oldPointer)[-1];
650+
auto oldSizePointer = ((void**)oldPointer)[-2];
651+
652+
// Calculate the actual old data size (excluding the header).
653+
size_t oldSize = (char*)oldSizePointer - (char*)oldAllocatedMemPointer - 2 * sizeof(void*);
654+
655+
// Allocate new memory.
648656
auto reallocatedPointer = RoaringMallocUdf(newSize);
649-
auto oldAllocatedMemPointer = (char*)((void**)oldPointer)[-1];
650-
auto oldSizePointer = (char*)((void**)oldPointer)[-2];
651-
memcpy(reallocatedPointer, oldPointer, oldSizePointer - oldAllocatedMemPointer);
657+
658+
// Copy the minimum of old size and new size.
659+
size_t copySize = oldSize < newSize ? oldSize : newSize;
660+
memcpy(reallocatedPointer, oldPointer, copySize);
661+
662+
// Free the old memory.
652663
RoaringFreeUdf(oldPointer);
653664

654665
return reallocatedPointer;

0 commit comments

Comments
 (0)