From 59b2824d46c3162cb87e1f076cf7e658620251be Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Tue, 25 Jun 2013 06:59:27 -0700 Subject: [PATCH] Fix Clang static analysis warnings --- .../modules/beast_core/containers/beast_Array.h | 16 +++++++++++----- .../containers/beast_ArrayAllocationBase.h | 3 ++- .../beast_core/containers/beast_OwnedArray.h | 16 ++++++++++++---- .../containers/beast_ReferenceCountedArray.h | 4 +++- .../beast_core/system/beast_PlatformDefs.h | 15 +++++++++++++-- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/Subtrees/beast/modules/beast_core/containers/beast_Array.h b/Subtrees/beast/modules/beast_core/containers/beast_Array.h index 1cec6d7fd..9e8e13e7f 100644 --- a/Subtrees/beast/modules/beast_core/containers/beast_Array.h +++ b/Subtrees/beast/modules/beast_core/containers/beast_Array.h @@ -230,8 +230,14 @@ public: ElementType operator[] (const int index) const { 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. @@ -246,7 +252,7 @@ public: inline ElementType getUnchecked (const int index) const { const ScopedLockType lock (getLock()); - bassert (isPositiveAndBelow (index, numUsed)); + bassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr); return data.elements [index]; } @@ -262,7 +268,7 @@ public: inline ElementType& getReference (const int index) const noexcept { const ScopedLockType lock (getLock()); - bassert (isPositiveAndBelow (index, numUsed)); + bassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr); return data.elements [index]; } @@ -383,6 +389,7 @@ public: { const ScopedLockType lock (getLock()); data.ensureAllocatedSize (numUsed + 1); + bassert (data.elements != nullptr); if (isPositiveAndBelow (indexToInsertAt, numUsed)) { @@ -1043,5 +1050,4 @@ private: } }; - #endif // BEAST_ARRAY_BEASTHEADER diff --git a/Subtrees/beast/modules/beast_core/containers/beast_ArrayAllocationBase.h b/Subtrees/beast/modules/beast_core/containers/beast_ArrayAllocationBase.h index 479fb9091..9f1ffe114 100644 --- a/Subtrees/beast/modules/beast_core/containers/beast_ArrayAllocationBase.h +++ b/Subtrees/beast/modules/beast_core/containers/beast_ArrayAllocationBase.h @@ -103,6 +103,8 @@ public: { if (minNumElements > numAllocated) setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7); + + bassert (numAllocated <= 0 || elements != nullptr); } /** Minimises the amount of storage allocated so that it's no more than @@ -129,5 +131,4 @@ private: BEAST_DECLARE_NON_COPYABLE (ArrayAllocationBase) }; - #endif // BEAST_ARRAYALLOCATIONBASE_BEASTHEADER diff --git a/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h b/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h index e4a13429f..b029269da 100644 --- a/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h +++ b/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h @@ -124,8 +124,13 @@ public: inline ObjectClass* operator[] (const int index) const noexcept { const ScopedLockType lock (getLock()); - return isPositiveAndBelow (index, numUsed) ? data.elements [index] - : static_cast (nullptr); + if (isPositiveAndBelow (index, numUsed)) + { + 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. @@ -136,7 +141,7 @@ public: inline ObjectClass* getUnchecked (const int index) const noexcept { const ScopedLockType lock (getLock()); - bassert (isPositiveAndBelow (index, numUsed)); + bassert (isPositiveAndBelow (index, numUsed) && data.elements != nullptr); return data.elements [index]; } @@ -243,6 +248,7 @@ public: { const ScopedLockType lock (getLock()); data.ensureAllocatedSize (numUsed + 1); + bassert (data.elements != nullptr); data.elements [numUsed++] = const_cast (newObject); } @@ -274,6 +280,7 @@ public: indexToInsertAt = numUsed; data.ensureAllocatedSize (numUsed + 1); + bassert (data.elements != nullptr); ObjectClass** const e = data.elements + indexToInsertAt; const int numToMove = numUsed - indexToInsertAt; @@ -426,6 +433,7 @@ public: numElementsToAdd = arrayToAddFrom.size() - startIndex; data.ensureAllocatedSize (numUsed + numElementsToAdd); + bassert (data.elements != nullptr); while (--numElementsToAdd >= 0) { @@ -466,6 +474,7 @@ public: numElementsToAdd = arrayToAddFrom.size() - startIndex; data.ensureAllocatedSize (numUsed + numElementsToAdd); + bassert (data.elements != nullptr); while (--numElementsToAdd >= 0) { @@ -861,5 +870,4 @@ private: BEAST_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OwnedArray) }; - #endif // BEAST_OWNEDARRAY_BEASTHEADER diff --git a/Subtrees/beast/modules/beast_core/containers/beast_ReferenceCountedArray.h b/Subtrees/beast/modules/beast_core/containers/beast_ReferenceCountedArray.h index 9010b78fa..b03559170 100644 --- a/Subtrees/beast/modules/beast_core/containers/beast_ReferenceCountedArray.h +++ b/Subtrees/beast/modules/beast_core/containers/beast_ReferenceCountedArray.h @@ -292,6 +292,7 @@ public: { const ScopedLockType lock (getLock()); data.ensureAllocatedSize (numUsed + 1); + bassert (data.elements != nullptr); data.elements [numUsed++] = newObject; if (newObject != nullptr) @@ -322,6 +323,7 @@ public: indexToInsertAt = numUsed; data.ensureAllocatedSize (numUsed + 1); + bassert (data.elements != nullptr); ObjectClass** const e = data.elements + indexToInsertAt; const int numToMove = numUsed - indexToInsertAt; @@ -388,6 +390,7 @@ public: else { data.ensureAllocatedSize (numUsed + 1); + bassert (data.elements != nullptr); data.elements [numUsed++] = newObject; } } @@ -851,5 +854,4 @@ private: int numUsed; }; - #endif // BEAST_REFERENCECOUNTEDARRAY_BEASTHEADER diff --git a/Subtrees/beast/modules/beast_core/system/beast_PlatformDefs.h b/Subtrees/beast/modules/beast_core/system/beast_PlatformDefs.h index dd3ff1051..59b93ba3c 100644 --- a/Subtrees/beast/modules/beast_core/system/beast_PlatformDefs.h +++ b/Subtrees/beast/modules/beast_core/system/beast_PlatformDefs.h @@ -78,6 +78,17 @@ #define beast_breakDebugger { __asm int 3 } #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 @@ -92,9 +103,9 @@ It is only compiled in a debug build, (unless BEAST_LOG_ASSERTIONS is enabled for your build). @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. This macro gets turned into a no-op when you're building with debugging turned off, so be