Skip to content

Commit 008a081

Browse files
fix: avoid calling free on non-heap object
Signed-off-by: Mateusz Jablonski <[email protected]> Source: ebfa384
1 parent 7271050 commit 008a081

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

shared/source/utilities/stackvec.h

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2023 Intel Corporation
2+
* Copyright (C) 2018-2024 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -40,16 +40,15 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
4040
static constexpr SizeT onStackCaps = onStackCapacity;
4141

4242
StackVec() {
43-
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
43+
switchToStackMem();
4444
}
4545

4646
template <typename ItType>
4747
StackVec(ItType beginIt, ItType endIt) {
48-
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
48+
switchToStackMem();
4949
size_t count = (endIt - beginIt);
5050
if (count > onStackCapacity) {
5151
dynamicMem = new std::vector<DataType>(beginIt, endIt);
52-
setUsesDynamicMem();
5352
return;
5453
}
5554

@@ -61,10 +60,9 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
6160
}
6261

6362
StackVec(const StackVec &rhs) {
64-
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
63+
switchToStackMem();
6564
if (onStackCaps < rhs.size()) {
6665
dynamicMem = new std::vector<DataType>(rhs.begin(), rhs.end());
67-
setUsesDynamicMem();
6866
return;
6967
}
7068

@@ -75,12 +73,12 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
7573

7674
explicit StackVec(size_t initialSize)
7775
: StackVec() {
78-
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
76+
switchToStackMem();
7977
resize(initialSize);
8078
}
8179

8280
StackVec(std::initializer_list<DataType> init) {
83-
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
81+
switchToStackMem();
8482
reserve(init.size());
8583
for (const auto &obj : init) {
8684
push_back(obj);
@@ -100,7 +98,6 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
10098

10199
if (onStackCaps < rhs.size()) {
102100
this->dynamicMem = new std::vector<DataType>(rhs.begin(), rhs.end());
103-
setUsesDynamicMem();
104101
return *this;
105102
}
106103

@@ -115,8 +112,7 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
115112
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
116113
if (rhs.usesDynamicMem()) {
117114
this->dynamicMem = rhs.dynamicMem;
118-
setUsesDynamicMem();
119-
rhs.onStackSize = 0U;
115+
rhs.switchToStackMem();
120116
return;
121117
}
122118

@@ -138,8 +134,7 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
138134
delete this->dynamicMem;
139135
}
140136
this->dynamicMem = rhs.dynamicMem;
141-
this->setUsesDynamicMem();
142-
rhs.onStackSize = 0U;
137+
rhs.switchToStackMem();
143138
return *this;
144139
}
145140

@@ -334,7 +329,7 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
334329
}
335330

336331
bool usesDynamicMem() const {
337-
return std::numeric_limits<decltype(onStackSize)>::max() == this->onStackSize;
332+
return reinterpret_cast<uintptr_t>(this->onStackMem) != reinterpret_cast<uintptr_t>(onStackMemRawBytes) && this->dynamicMem;
338333
}
339334

340335
auto data() {
@@ -347,9 +342,6 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
347342
private:
348343
template <typename RhsDataType, size_t rhsOnStackCapacity, typename RhsStackSizeT>
349344
friend class StackVec;
350-
void setUsesDynamicMem() {
351-
this->onStackSize = std::numeric_limits<decltype(onStackSize)>::max();
352-
}
353345

354346
void resizeImpl(size_t newSize, const DataType *value) {
355347
// new size does not fit into internal mem
@@ -408,7 +400,6 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
408400
}
409401
clearStackObjects();
410402
}
411-
setUsesDynamicMem();
412403
}
413404

414405
void clearStackObjects() {
@@ -422,6 +413,9 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
422413
it->~DataType();
423414
}
424415
}
416+
void switchToStackMem() {
417+
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
418+
}
425419

426420
union {
427421
std::vector<DataType> *dynamicMem;

0 commit comments

Comments
 (0)