From 9e373489980af06510ac671c9b2fcb688c03103a Mon Sep 17 00:00:00 2001 From: chenkeyu Date: Tue, 11 Jun 2024 18:16:17 +0800 Subject: [PATCH] Replace realloc with malloc and memcpy_s Issue:https://gitee.com/openharmony/commonlibrary_c_utils/issues/IA4BBV Signed-off-by: chenkeyu --- base/src/parcel.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/base/src/parcel.cpp b/base/src/parcel.cpp index ef607c5..6d7b41a 100644 --- a/base/src/parcel.cpp +++ b/base/src/parcel.cpp @@ -636,11 +636,20 @@ bool Parcel::EnsureObjectsCapacity() size_t newCapacity = ((objectsCapacity_ + NEW_CAPACITY_ADD) * NEW_CAPACITY_MULTI) / NEW_CAPACITY_DIV; size_t newBytes = newCapacity * sizeof(binder_size_t); - void *newOffsets = realloc(objectOffsets_, newBytes); + void *newOffsets = malloc(newBytes); if (newOffsets == nullptr) { return false; } + if (objectOffsets_ != nullptr) { + if (memcpy_s(newOffsets, newBytes, objectOffsets_, objectCursor_ * sizeof(binder_size_t)) != EOK) { + free(newOffsets); + newOffsets = nullptr; + return false; + } + free(objectOffsets_); + } + objectOffsets_ = reinterpret_cast(newOffsets); objectsCapacity_ = newCapacity; return true; @@ -876,10 +885,18 @@ bool Parcel::RewindWrite(size_t newPosition) return true; } size_t newBytes = objectSize * sizeof(binder_size_t); - void *newOffsets = realloc(objectOffsets_, newBytes); + void *newOffsets = malloc(newBytes); if (newOffsets == nullptr) { return false; } + if (objectOffsets_ != nullptr) { + if (memcpy_s(newOffsets, newBytes, objectOffsets_, newBytes) != EOK) { + free(newOffsets); + newOffsets = nullptr; + return false; + } + free(objectOffsets_); + } objectOffsets_ = reinterpret_cast(newOffsets); objectCursor_ = objectSize; objectsCapacity_ = objectCursor_; -- Gitee