Browse Source

Added assertions to some array methods to avoid false alarms from Clang static analyser.

tags/2021-05-28
jules 11 years ago
parent
commit
0ff1e14b8b
3 changed files with 74 additions and 34 deletions
  1. +16
    -2
      modules/juce_core/containers/juce_Array.h
  2. +16
    -4
      modules/juce_core/containers/juce_OwnedArray.h
  3. +42
    -28
      modules/juce_core/containers/juce_ReferenceCountedArray.h

+ 16
- 2
modules/juce_core/containers/juce_Array.h View File

@@ -279,7 +279,14 @@ public:
inline ElementType getFirst() const
{
const ScopedLockType lock (getLock());
return numUsed > 0 ? data.elements[0] : ElementType();
if (numUsed > 0)
{
jassert (data.elements != nullptr);
return data.elements[0];
}
return ElementType();
}
/** Returns the last element in the array, or a default value if the array is empty.
@@ -289,7 +296,14 @@ public:
inline ElementType getLast() const
{
const ScopedLockType lock (getLock());
return numUsed > 0 ? data.elements[numUsed - 1] : ElementType();
if (numUsed > 0)
{
jassert (data.elements != nullptr);
return data.elements[numUsed - 1];
}
return ElementType();
}
/** Returns a pointer to the actual array data.


+ 16
- 4
modules/juce_core/containers/juce_OwnedArray.h View File

@@ -166,8 +166,14 @@ public:
inline ObjectClass* getFirst() const noexcept
{
const ScopedLockType lock (getLock());
return numUsed > 0 ? data.elements [0]
: static_cast<ObjectClass*> (nullptr);
if (numUsed > 0)
{
jassert (data.elements != nullptr);
return data.elements [0];
}
return nullptr;
}
/** Returns a pointer to the last object in the array.
@@ -178,8 +184,14 @@ public:
inline ObjectClass* getLast() const noexcept
{
const ScopedLockType lock (getLock());
return numUsed > 0 ? data.elements [numUsed - 1]
: static_cast<ObjectClass*> (nullptr);
if (numUsed > 0)
{
jassert (data.elements != nullptr);
return data.elements [numUsed - 1];
}
return nullptr;
}
/** Returns a pointer to the actual array data.


+ 42
- 28
modules/juce_core/containers/juce_ReferenceCountedArray.h View File

@@ -180,8 +180,14 @@ public:
inline ObjectClass* getObjectPointer (const int index) const noexcept
{
const ScopedLockType lock (getLock());
return isPositiveAndBelow (index, numUsed) ? data.elements [index]
: nullptr;
if (isPositiveAndBelow (index, numUsed))
{
jassert (data.elements != nullptr);
return data.elements [index];
}
return ObjectClassPtr();
}
/** Returns a raw pointer to the object at this index in the array, without checking
@@ -190,7 +196,7 @@ public:
inline ObjectClass* getObjectPointerUnchecked (const int index) const noexcept
{
const ScopedLockType lock (getLock());
jassert (isPositiveAndBelow (index, numUsed));
jassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr);
return data.elements [index];
}
@@ -202,8 +208,14 @@ public:
inline ObjectClassPtr getFirst() const noexcept
{
const ScopedLockType lock (getLock());
return numUsed > 0 ? data.elements [0]
: static_cast <ObjectClass*> (nullptr);
if (numUsed > 0)
{
jassert (data.elements != nullptr);
return data.elements [0];
}
return ObjectClassPtr();
}
/** Returns a pointer to the last object in the array.
@@ -214,8 +226,14 @@ public:
inline ObjectClassPtr getLast() const noexcept
{
const ScopedLockType lock (getLock());
return numUsed > 0 ? data.elements [numUsed - 1]
: static_cast <ObjectClass*> (nullptr);
if (numUsed > 0)
{
jassert (data.elements != nullptr);
return data.elements [numUsed - 1];
}
return ObjectClassPtr();
}
/** Returns a pointer to the actual array data.
@@ -325,35 +343,31 @@ public:
ObjectClass* insert (int indexToInsertAt,
ObjectClass* const newObject) noexcept
{
if (indexToInsertAt >= 0)
{
const ScopedLockType lock (getLock());
if (indexToInsertAt < 0)
return add (newObject);
const ScopedLockType lock (getLock());
if (indexToInsertAt > numUsed)
indexToInsertAt = numUsed;
if (indexToInsertAt > numUsed)
indexToInsertAt = numUsed;
data.ensureAllocatedSize (numUsed + 1);
jassert (data.elements != nullptr);
data.ensureAllocatedSize (numUsed + 1);
jassert (data.elements != nullptr);
ObjectClass** const e = data.elements + indexToInsertAt;
const int numToMove = numUsed - indexToInsertAt;
ObjectClass** const e = data.elements + indexToInsertAt;
const int numToMove = numUsed - indexToInsertAt;
if (numToMove > 0)
memmove (e + 1, e, sizeof (ObjectClass*) * (size_t) numToMove);
if (numToMove > 0)
memmove (e + 1, e, sizeof (ObjectClass*) * (size_t) numToMove);
*e = newObject;
*e = newObject;
if (newObject != nullptr)
newObject->incReferenceCount();
if (newObject != nullptr)
newObject->incReferenceCount();
++numUsed;
++numUsed;
return newObject;
}
else
{
return add (newObject);
}
return newObject;
}
/** Appends a new object at the end of the array as long as the array doesn't


Loading…
Cancel
Save