From 1ca1e1d4436ea88a390ac71f6f936e7ac1e9e6cb Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Tue, 23 Mar 2021 19:14:11 +0100 Subject: [PATCH] Fix the Buffer-related issue --- src/sfizz/Buffer.h | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/sfizz/Buffer.h b/src/sfizz/Buffer.h index 0fd8dc29..1f2dd2d3 100644 --- a/src/sfizz/Buffer.h +++ b/src/sfizz/Buffer.h @@ -156,8 +156,12 @@ public: return true; } - auto tempSize = newSize + 2 * AlignmentMask; // To ensure that we have leeway at the beginning and at the end - auto* newData = std::realloc(paddedData.get(), tempSize * sizeof(value_type)); + Type* oldData = paddedData.get(); + Type* oldNormalData = normalData; + std::size_t oldSize = alignedSize; + + std::size_t tempSize = newSize + 2 * AlignmentMask; // To ensure that we have leeway at the beginning and at the end + Type* newData = reinterpret_cast(std::calloc(tempSize, sizeof(value_type))); if (newData == nullptr) { return false; } @@ -173,12 +177,15 @@ public: paddedData.reset(static_cast(newData)); normalData = static_cast(align(Alignment, alignedSize, newData, tempSize)); normalEnd = normalData + alignedSize; - auto endMisalignment = (alignedSize & TypeAlignmentMask); + std::size_t endMisalignment = (alignedSize & TypeAlignmentMask); if (endMisalignment != 0) _alignedEnd = normalEnd + Alignment - endMisalignment; else _alignedEnd = normalEnd; + std::memcpy(normalData, oldNormalData, std::min(newSize, oldSize) * sizeof(Type)); + std::free(oldData); + return true; } @@ -307,14 +314,14 @@ private: static_assert(Alignment == 0 || Alignment == 4 || Alignment == 8 || Alignment == 16 || Alignment == 32, "Bad alignment value"); static_assert(TypeAlignment * sizeof(value_type) == Alignment || !std::is_arithmetic::value, "The alignment does not appear to be divided by the size of the arithmetic Type"); - void* align(std::size_t alignment, std::size_t size, void *&ptr, std::size_t &space ) + void* align(std::size_t alignment, std::size_t size, void *ptr, std::size_t &space) { - std::uintptr_t pn = reinterpret_cast< std::uintptr_t>( ptr ); - std::uintptr_t aligned = ( pn + alignment - 1 ) & - alignment; + std::uintptr_t pn = reinterpret_cast(ptr); + std::uintptr_t aligned = (pn + alignment - 1) & - alignment; std::size_t padding = aligned - pn; - if ( space < size + padding ) return nullptr; + if (space < size + padding) return nullptr; space -= padding; - return ptr = reinterpret_cast< void * >( aligned ); + return reinterpret_cast(aligned); } struct deleter {