From 869a3090194a38aa8748ad0e9fc8f26d707f17eb Mon Sep 17 00:00:00 2001 From: huangyicong Date: Wed, 27 Apr 2022 15:29:59 +0800 Subject: [PATCH 1/2] fix realloc Signed-off-by: huangyicong --- base/src/parcel.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/base/src/parcel.cpp b/base/src/parcel.cpp index 855f7cf..6fdaaaa 100644 --- a/base/src/parcel.cpp +++ b/base/src/parcel.cpp @@ -13,9 +13,10 @@ * limitations under the License. */ -#include "parcel.h" +#include #include "securec.h" #include "utils_log.h" +#include "parcel.h" namespace OHOS { @@ -580,7 +581,7 @@ 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 = allocator_->Realloc(objectOffsets_, newBytes); if (newOffsets == nullptr) { return false; } @@ -1131,7 +1132,21 @@ void DefaultAllocator::Dealloc(void *data) void *DefaultAllocator::Realloc(void *data, size_t newSize) { - return realloc(data, newSize); + if (newSize != 0) { + void *newData = malloc(newSize); + if (newData != nullptr) { + if (data == nullptr) { + return newData; + } + if (memcpy_s(newData, newSize, data, malloc_usable_size(data)) == EOK) { + free(data); + return newData; + } + free(newData); + } + UTILS_LOGW("Realloc failed!"); + } + return nullptr; } template -- Gitee From d364ccceb7e974598eb4f77bab8f06ff9e3208ba Mon Sep 17 00:00:00 2001 From: huangyicong Date: Wed, 27 Apr 2022 18:20:43 +0800 Subject: [PATCH 2/2] fix realloc Signed-off-by: huangyicong --- base/src/parcel.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/src/parcel.cpp b/base/src/parcel.cpp index 6fdaaaa..ddabc9c 100644 --- a/base/src/parcel.cpp +++ b/base/src/parcel.cpp @@ -1133,6 +1133,7 @@ void DefaultAllocator::Dealloc(void *data) void *DefaultAllocator::Realloc(void *data, size_t newSize) { if (newSize != 0) { + // newSize checked before Realloc. void *newData = malloc(newSize); if (newData != nullptr) { if (data == nullptr) { @@ -1144,8 +1145,8 @@ void *DefaultAllocator::Realloc(void *data, size_t newSize) } free(newData); } - UTILS_LOGW("Realloc failed!"); } + UTILS_LOGW("Realloc failed! newSize = %{public}zu", newSize); return nullptr; } -- Gitee