From 008a0818ba10ab54c06dcbbd350faf1107cc65aa Mon Sep 17 00:00:00 2001 From: Mateusz Jablonski Date: Fri, 13 Sep 2024 14:30:14 +0000 Subject: [PATCH] fix: avoid calling free on non-heap object Signed-off-by: Mateusz Jablonski Source: ebfa384a74ed6e32dfe65ce51a4bd0a2a3847671 --- shared/source/utilities/stackvec.h | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/shared/source/utilities/stackvec.h b/shared/source/utilities/stackvec.h index 603ed55dc1ab8..a4c843bb008ab 100644 --- a/shared/source/utilities/stackvec.h +++ b/shared/source/utilities/stackvec.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2023 Intel Corporation + * Copyright (C) 2018-2024 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -40,16 +40,15 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding) static constexpr SizeT onStackCaps = onStackCapacity; StackVec() { - onStackMem = reinterpret_cast(onStackMemRawBytes); + switchToStackMem(); } template StackVec(ItType beginIt, ItType endIt) { - onStackMem = reinterpret_cast(onStackMemRawBytes); + switchToStackMem(); size_t count = (endIt - beginIt); if (count > onStackCapacity) { dynamicMem = new std::vector(beginIt, endIt); - setUsesDynamicMem(); return; } @@ -61,10 +60,9 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding) } StackVec(const StackVec &rhs) { - onStackMem = reinterpret_cast(onStackMemRawBytes); + switchToStackMem(); if (onStackCaps < rhs.size()) { dynamicMem = new std::vector(rhs.begin(), rhs.end()); - setUsesDynamicMem(); return; } @@ -75,12 +73,12 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding) explicit StackVec(size_t initialSize) : StackVec() { - onStackMem = reinterpret_cast(onStackMemRawBytes); + switchToStackMem(); resize(initialSize); } StackVec(std::initializer_list init) { - onStackMem = reinterpret_cast(onStackMemRawBytes); + switchToStackMem(); reserve(init.size()); for (const auto &obj : init) { push_back(obj); @@ -100,7 +98,6 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding) if (onStackCaps < rhs.size()) { this->dynamicMem = new std::vector(rhs.begin(), rhs.end()); - setUsesDynamicMem(); return *this; } @@ -115,8 +112,7 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding) onStackMem = reinterpret_cast(onStackMemRawBytes); if (rhs.usesDynamicMem()) { this->dynamicMem = rhs.dynamicMem; - setUsesDynamicMem(); - rhs.onStackSize = 0U; + rhs.switchToStackMem(); return; } @@ -138,8 +134,7 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding) delete this->dynamicMem; } this->dynamicMem = rhs.dynamicMem; - this->setUsesDynamicMem(); - rhs.onStackSize = 0U; + rhs.switchToStackMem(); return *this; } @@ -334,7 +329,7 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding) } bool usesDynamicMem() const { - return std::numeric_limits::max() == this->onStackSize; + return reinterpret_cast(this->onStackMem) != reinterpret_cast(onStackMemRawBytes) && this->dynamicMem; } auto data() { @@ -347,9 +342,6 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding) private: template friend class StackVec; - void setUsesDynamicMem() { - this->onStackSize = std::numeric_limits::max(); - } void resizeImpl(size_t newSize, const DataType *value) { // new size does not fit into internal mem @@ -408,7 +400,6 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding) } clearStackObjects(); } - setUsesDynamicMem(); } void clearStackObjects() { @@ -422,6 +413,9 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding) it->~DataType(); } } + void switchToStackMem() { + onStackMem = reinterpret_cast(onStackMemRawBytes); + } union { std::vector *dynamicMem;