Skip to content

Commit b15f46a

Browse files
authored
Merge pull request InsightSoftwareConsortium#5713 from N-Dekker/Make-VariableLengthVector-m_Data-null
2 parents f3697ed + 1d87efa commit b15f46a

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

Modules/Core/Common/include/itkVariableLengthVector.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,13 @@ class ITK_TEMPLATE_EXPORT VariableLengthVector
270270
void
271271
operator()(unsigned int newSize, unsigned int oldSize, TValue2 * oldBuffer, TValue2 * newBuffer) const
272272
{
273-
itkAssertInDebugAndIgnoreInReleaseMacro(newBuffer);
274273
const size_t nb = std::min(newSize, oldSize);
275-
itkAssertInDebugAndIgnoreInReleaseMacro(nb == 0 || (nb > 0 && oldBuffer != nullptr));
276-
std::copy_n(oldBuffer, nb, newBuffer);
274+
if (nb > 0)
275+
{
276+
itkAssertInDebugAndIgnoreInReleaseMacro(newBuffer);
277+
itkAssertInDebugAndIgnoreInReleaseMacro(oldBuffer);
278+
std::copy_n(oldBuffer, nb, newBuffer);
279+
}
277280
}
278281
};
279282

Modules/Core/Common/include/itkVariableLengthVector.hxx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,15 @@ namespace itk
2929

3030
template <typename TValue>
3131
VariableLengthVector<TValue>::VariableLengthVector(unsigned int length)
32-
: m_Data(new TValue[length])
32+
: m_Data(length == 0 ? nullptr : new TValue[length])
3333
, m_NumElements(length)
34-
{
35-
// postcondition(s)
36-
itkAssertInDebugAndIgnoreInReleaseMacro(m_Data != nullptr);
37-
}
34+
{}
3835

3936
template <typename TValue>
4037
VariableLengthVector<TValue>::VariableLengthVector(unsigned int length, const TValue & value)
4138
: VariableLengthVector(length)
4239
{
43-
std::fill_n(m_Data, length, value);
40+
Fill(value);
4441
}
4542

4643
template <typename TValue>
@@ -67,10 +64,6 @@ VariableLengthVector<TValue>::VariableLengthVector(const VariableLengthVector<TV
6764
itkAssertInDebugAndIgnoreInReleaseMacro(v.m_Data != nullptr);
6865
std::copy_n(&v.m_Data[0], m_NumElements, &this->m_Data[0]);
6966
}
70-
else
71-
{
72-
m_Data = nullptr;
73-
}
7467
}
7568

7669
template <typename TValue>
@@ -133,8 +126,6 @@ VariableLengthVector<TValue>::VariableLengthVector(
133126
rhs)
134127
: VariableLengthVector(rhs.Size())
135128
{
136-
// VariableLengthVector(length) post-condition
137-
itkAssertInDebugAndIgnoreInReleaseMacro(m_Data != nullptr);
138129
for (ElementIdentifier i = 0; i < m_NumElements; ++i)
139130
{
140131
this->m_Data[i] = static_cast<TValue>(rhs[i]);
@@ -191,11 +182,14 @@ VariableLengthVector<TValue>::Reserve(ElementIdentifier size)
191182
}
192183
else
193184
{
194-
m_Data = new TValue[size];
185+
// At this point, m_Data is null.
186+
if (size > 0)
187+
{
188+
m_Data = new TValue[size];
189+
}
195190
m_NumElements = size;
196191
m_LetArrayManageMemory = true;
197192
}
198-
itkAssertInDebugAndIgnoreInReleaseMacro(m_Data != nullptr);
199193
}
200194

201195
#ifndef ITK_FUTURE_LEGACY_REMOVE
@@ -274,10 +268,15 @@ VariableLengthVector<TValue>::SetSize(unsigned int sz, TReallocatePolicy realloc
274268

275269
if (reallocatePolicy(sz, m_NumElements) || !m_LetArrayManageMemory)
276270
{
277-
auto temp = make_unique_for_overwrite<TValue[]>(sz); // may throw
278-
itkAssertInDebugAndIgnoreInReleaseMacro(temp);
279-
itkAssertInDebugAndIgnoreInReleaseMacro(m_NumElements == 0 || (m_NumElements > 0 && m_Data != nullptr));
280-
keepValues(sz, m_NumElements, m_Data, temp.get());
271+
std::unique_ptr<TValue[]> temp;
272+
273+
if (sz > 0)
274+
{
275+
temp = make_unique_for_overwrite<TValue[]>(sz); // may throw
276+
itkAssertInDebugAndIgnoreInReleaseMacro(temp);
277+
itkAssertInDebugAndIgnoreInReleaseMacro(m_NumElements == 0 || (m_NumElements > 0 && m_Data != nullptr));
278+
keepValues(sz, m_NumElements, m_Data, temp.get());
279+
}
281280
// commit changes
282281
if (m_LetArrayManageMemory)
283282
{
@@ -293,10 +292,11 @@ template <typename TValue>
293292
void
294293
VariableLengthVector<TValue>::Fill(const TValue & v)
295294
{
296-
itkAssertInDebugAndIgnoreInReleaseMacro(m_NumElements == 0 || (m_NumElements > 0 && m_Data != nullptr));
297-
// VC++ version of std::fill_n() expects the output iterator to be valid
298-
// instead of expecting the range [OutIt, OutIt+n) to be valid.
299-
std::fill(&this->m_Data[0], &this->m_Data[m_NumElements], v);
295+
if (m_NumElements > 0)
296+
{
297+
itkAssertInDebugAndIgnoreInReleaseMacro(m_Data);
298+
std::fill_n(m_Data, m_NumElements, v);
299+
}
300300
}
301301

302302
template <typename TValue>

0 commit comments

Comments
 (0)