Fix Clang static analysis warnings

This commit is contained in:
Vinnie Falco
2013-06-25 06:59:27 -07:00
parent 2bc0ef9e43
commit 59b2824d46
5 changed files with 41 additions and 13 deletions

View File

@@ -230,8 +230,14 @@ public:
ElementType operator[] (const int index) const ElementType operator[] (const int index) const
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
return isPositiveAndBelow (index, numUsed) ? data.elements [index]
: ElementType(); if (isPositiveAndBelow (index, numUsed))
{
bassert (data.elements != nullptr);
return data.elements [index];
}
return ElementType();
} }
/** Returns one of the elements in the array, without checking the index passed in. /** Returns one of the elements in the array, without checking the index passed in.
@@ -246,7 +252,7 @@ public:
inline ElementType getUnchecked (const int index) const inline ElementType getUnchecked (const int index) const
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
bassert (isPositiveAndBelow (index, numUsed)); bassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr);
return data.elements [index]; return data.elements [index];
} }
@@ -262,7 +268,7 @@ public:
inline ElementType& getReference (const int index) const noexcept inline ElementType& getReference (const int index) const noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
bassert (isPositiveAndBelow (index, numUsed)); bassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr);
return data.elements [index]; return data.elements [index];
} }
@@ -383,6 +389,7 @@ public:
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
data.ensureAllocatedSize (numUsed + 1); data.ensureAllocatedSize (numUsed + 1);
bassert (data.elements != nullptr);
if (isPositiveAndBelow (indexToInsertAt, numUsed)) if (isPositiveAndBelow (indexToInsertAt, numUsed))
{ {
@@ -1043,5 +1050,4 @@ private:
} }
}; };
#endif // BEAST_ARRAY_BEASTHEADER #endif // BEAST_ARRAY_BEASTHEADER

View File

@@ -103,6 +103,8 @@ public:
{ {
if (minNumElements > numAllocated) if (minNumElements > numAllocated)
setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7); setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
bassert (numAllocated <= 0 || elements != nullptr);
} }
/** Minimises the amount of storage allocated so that it's no more than /** Minimises the amount of storage allocated so that it's no more than
@@ -129,5 +131,4 @@ private:
BEAST_DECLARE_NON_COPYABLE (ArrayAllocationBase) BEAST_DECLARE_NON_COPYABLE (ArrayAllocationBase)
}; };
#endif // BEAST_ARRAYALLOCATIONBASE_BEASTHEADER #endif // BEAST_ARRAYALLOCATIONBASE_BEASTHEADER

View File

@@ -124,8 +124,13 @@ public:
inline ObjectClass* operator[] (const int index) const noexcept inline ObjectClass* operator[] (const int index) const noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
return isPositiveAndBelow (index, numUsed) ? data.elements [index] if (isPositiveAndBelow (index, numUsed))
: static_cast <ObjectClass*> (nullptr); {
bassert (data.elements != nullptr);
return data.elements [index];
}
return nullptr;
} }
/** Returns a pointer to the object at this index in the array, without checking whether the index is in-range. /** Returns a pointer to the object at this index in the array, without checking whether the index is in-range.
@@ -136,7 +141,7 @@ public:
inline ObjectClass* getUnchecked (const int index) const noexcept inline ObjectClass* getUnchecked (const int index) const noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
bassert (isPositiveAndBelow (index, numUsed)); bassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr);
return data.elements [index]; return data.elements [index];
} }
@@ -243,6 +248,7 @@ public:
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
data.ensureAllocatedSize (numUsed + 1); data.ensureAllocatedSize (numUsed + 1);
bassert (data.elements != nullptr);
data.elements [numUsed++] = const_cast <ObjectClass*> (newObject); data.elements [numUsed++] = const_cast <ObjectClass*> (newObject);
} }
@@ -274,6 +280,7 @@ public:
indexToInsertAt = numUsed; indexToInsertAt = numUsed;
data.ensureAllocatedSize (numUsed + 1); data.ensureAllocatedSize (numUsed + 1);
bassert (data.elements != nullptr);
ObjectClass** const e = data.elements + indexToInsertAt; ObjectClass** const e = data.elements + indexToInsertAt;
const int numToMove = numUsed - indexToInsertAt; const int numToMove = numUsed - indexToInsertAt;
@@ -426,6 +433,7 @@ public:
numElementsToAdd = arrayToAddFrom.size() - startIndex; numElementsToAdd = arrayToAddFrom.size() - startIndex;
data.ensureAllocatedSize (numUsed + numElementsToAdd); data.ensureAllocatedSize (numUsed + numElementsToAdd);
bassert (data.elements != nullptr);
while (--numElementsToAdd >= 0) while (--numElementsToAdd >= 0)
{ {
@@ -466,6 +474,7 @@ public:
numElementsToAdd = arrayToAddFrom.size() - startIndex; numElementsToAdd = arrayToAddFrom.size() - startIndex;
data.ensureAllocatedSize (numUsed + numElementsToAdd); data.ensureAllocatedSize (numUsed + numElementsToAdd);
bassert (data.elements != nullptr);
while (--numElementsToAdd >= 0) while (--numElementsToAdd >= 0)
{ {
@@ -861,5 +870,4 @@ private:
BEAST_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OwnedArray) BEAST_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OwnedArray)
}; };
#endif // BEAST_OWNEDARRAY_BEASTHEADER #endif // BEAST_OWNEDARRAY_BEASTHEADER

View File

@@ -292,6 +292,7 @@ public:
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
data.ensureAllocatedSize (numUsed + 1); data.ensureAllocatedSize (numUsed + 1);
bassert (data.elements != nullptr);
data.elements [numUsed++] = newObject; data.elements [numUsed++] = newObject;
if (newObject != nullptr) if (newObject != nullptr)
@@ -322,6 +323,7 @@ public:
indexToInsertAt = numUsed; indexToInsertAt = numUsed;
data.ensureAllocatedSize (numUsed + 1); data.ensureAllocatedSize (numUsed + 1);
bassert (data.elements != nullptr);
ObjectClass** const e = data.elements + indexToInsertAt; ObjectClass** const e = data.elements + indexToInsertAt;
const int numToMove = numUsed - indexToInsertAt; const int numToMove = numUsed - indexToInsertAt;
@@ -388,6 +390,7 @@ public:
else else
{ {
data.ensureAllocatedSize (numUsed + 1); data.ensureAllocatedSize (numUsed + 1);
bassert (data.elements != nullptr);
data.elements [numUsed++] = newObject; data.elements [numUsed++] = newObject;
} }
} }
@@ -851,5 +854,4 @@ private:
int numUsed; int numUsed;
}; };
#endif // BEAST_REFERENCECOUNTEDARRAY_BEASTHEADER #endif // BEAST_REFERENCECOUNTEDARRAY_BEASTHEADER

View File

@@ -78,6 +78,17 @@
#define beast_breakDebugger { __asm int 3 } #define beast_breakDebugger { __asm int 3 }
#endif #endif
#if BEAST_CLANG && defined (__has_feature) && ! defined (BEAST_ANALYZER_NORETURN)
#if __has_feature (attribute_analyzer_noreturn)
inline void __attribute__((analyzer_noreturn)) beast_assert_noreturn() {}
#define BEAST_ANALYZER_NORETURN beast_assert_noreturn();
#endif
#endif
#ifndef BEAST_ANALYZER_NORETURN
#define BEAST_ANALYZER_NORETURN
#endif
//============================================================================== //==============================================================================
#if BEAST_DEBUG || DOXYGEN #if BEAST_DEBUG || DOXYGEN
@@ -92,9 +103,9 @@
It is only compiled in a debug build, (unless BEAST_LOG_ASSERTIONS is enabled for your build). It is only compiled in a debug build, (unless BEAST_LOG_ASSERTIONS is enabled for your build).
@see bassert @see bassert
*/ */
#define bassertfalse { beast_LogCurrentAssertion; if (beast::beast_isRunningUnderDebugger()) beast_breakDebugger; } #define bassertfalse { beast_LogCurrentAssertion; if (beast::beast_isRunningUnderDebugger()) beast_breakDebugger; BEAST_ANALYZER_NORETURN }
//============================================================================== //==============================================================================
/** Platform-independent assertion macro. /** Platform-independent assertion macro.
This macro gets turned into a no-op when you're building with debugging turned off, so be This macro gets turned into a no-op when you're building with debugging turned off, so be