From e4e1fbe0edf67bfe0697ef4f0122d144ae94815b Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Fri, 19 Jan 2018 19:42:03 -0500 Subject: [PATCH] Clear Coverity findings CID 186951, 186950, 186947 Coverity does not realize xorBlocks is always non-NULL when BT_XorInput is set --- adv-simd.h | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/adv-simd.h b/adv-simd.h index 657c1b90..c9632b0a 100644 --- a/adv-simd.h +++ b/adv-simd.h @@ -54,6 +54,8 @@ ANONYMOUS_NAMESPACE_BEGIN +using CryptoPP::byte; +using CryptoPP::word32; using CryptoPP::BlockTransformation; CRYPTOPP_CONSTANT(BT_XorInput = BlockTransformation::BT_XorInput) @@ -62,6 +64,18 @@ CRYPTOPP_CONSTANT(BT_InBlockIsCounter = BlockTransformation::BT_InBlockIsCounter CRYPTOPP_CONSTANT(BT_ReverseDirection = BlockTransformation::BT_ReverseDirection) CRYPTOPP_CONSTANT(BT_DontIncrementInOutPointers = BlockTransformation::BT_DontIncrementInOutPointers) +// Coverity finding on xorBlocks. While not obvious, xorBlocks is +// always non-NULL when BT_XorInput is set. All callers follow the +// convention. Also see https://stackoverflow.com/q/33719379/608639. +inline word32 XorBlocksToFlags(const byte* xorBlocks, word32 flags) +{ +#if defined(__COVERITY__) + return xorBlocks ? (flags) : (flags &= ~BT_XorInput); +#else + return CRYPTOPP_UNUSED(xorBlocks), flags; +#endif +} + ANONYMOUS_NAMESPACE_END // *************************** ARM NEON ************************** // @@ -109,6 +123,7 @@ inline size_t AdvancedProcessBlocks64_6x2_NEON(F2 func2, F6 func6, ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : neonBlockSize; ptrdiff_t xorIncrement = xorBlocks ? neonBlockSize : 0; ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : neonBlockSize; + flags = XorBlocksToFlags(xorBlocks, flags); // Coverity hack if (flags & BT_ReverseDirection) { @@ -337,6 +352,7 @@ size_t AdvancedProcessBlocks128_NEON1x6(F1 func1, F6 func6, ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0; ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; + flags = XorBlocksToFlags(xorBlocks, flags); // Coverity hack if (flags & BT_ReverseDirection) { @@ -476,6 +492,7 @@ size_t AdvancedProcessBlocks128_6x2_NEON(F2 func2, F6 func6, ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0; ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; + flags = XorBlocksToFlags(xorBlocks, flags); // Coverity hack if (flags & BT_ReverseDirection) { @@ -711,6 +728,7 @@ inline size_t GCC_NO_UBSAN AdvancedProcessBlocks64_6x2_SSE(F2 func2, F6 func6, ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : xmmBlockSize; ptrdiff_t xorIncrement = xorBlocks ? xmmBlockSize : 0; ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : xmmBlockSize; + flags = XorBlocksToFlags(xorBlocks, flags); // Coverity hack if (flags & BT_ReverseDirection) { @@ -765,8 +783,6 @@ inline size_t GCC_NO_UBSAN AdvancedProcessBlocks64_6x2_SSE(F2 func2, F6 func6, if (flags & BT_XorInput) { - // Coverity finding, appears to be false positive. Assert the condition. - CRYPTOPP_ASSERT(xorBlocks); block0 = _mm_xor_si128(block0, _mm_loadu_si128(CONST_M128_CAST(xorBlocks))); xorBlocks += xorIncrement; block1 = _mm_xor_si128(block1, _mm_loadu_si128(CONST_M128_CAST(xorBlocks))); @@ -844,8 +860,6 @@ inline size_t GCC_NO_UBSAN AdvancedProcessBlocks64_6x2_SSE(F2 func2, F6 func6, if (flags & BT_XorInput) { - // Coverity finding, appears to be false positive. Assert the condition. - CRYPTOPP_ASSERT(xorBlocks); block0 = _mm_xor_si128(block0, _mm_loadu_si128(CONST_M128_CAST(xorBlocks))); xorBlocks += xorIncrement; block1 = _mm_xor_si128(block1, _mm_loadu_si128(CONST_M128_CAST(xorBlocks))); @@ -945,6 +959,7 @@ inline size_t AdvancedProcessBlocks128_6x2_SSE(F2 func2, F6 func6, ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0; ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; + flags = XorBlocksToFlags(xorBlocks, flags); // Coverity hack if (flags & BT_ReverseDirection) { @@ -990,8 +1005,6 @@ inline size_t AdvancedProcessBlocks128_6x2_SSE(F2 func2, F6 func6, if (flags & BT_XorInput) { - // Coverity finding, appears to be false positive. Assert the condition. - CRYPTOPP_ASSERT(xorBlocks); block0 = _mm_xor_si128(block0, _mm_loadu_si128(CONST_M128_CAST(xorBlocks))); xorBlocks += xorIncrement; block1 = _mm_xor_si128(block1, _mm_loadu_si128(CONST_M128_CAST(xorBlocks))); @@ -1060,8 +1073,6 @@ inline size_t AdvancedProcessBlocks128_6x2_SSE(F2 func2, F6 func6, if (flags & BT_XorInput) { - // Coverity finding, appears to be false positive. Assert the condition. - CRYPTOPP_ASSERT(xorBlocks); block0 = _mm_xor_si128(block0, _mm_loadu_si128(CONST_M128_CAST(xorBlocks))); xorBlocks += xorIncrement; block1 = _mm_xor_si128(block1, _mm_loadu_si128(CONST_M128_CAST(xorBlocks))); @@ -1130,6 +1141,7 @@ inline size_t AdvancedProcessBlocks128_4x1_SSE(F1 func1, F4 func4, ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0; ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; + flags = XorBlocksToFlags(xorBlocks, flags); // Coverity hack if (flags & BT_ReverseDirection) { @@ -1169,8 +1181,6 @@ inline size_t AdvancedProcessBlocks128_4x1_SSE(F1 func1, F4 func4, if (flags & BT_XorInput) { - // Coverity finding, appears to be false positive. Assert the condition. - CRYPTOPP_ASSERT(xorBlocks); block0 = _mm_xor_si128(block0, _mm_loadu_si128(CONST_M128_CAST(xorBlocks))); xorBlocks += xorIncrement; block1 = _mm_xor_si128(block1, _mm_loadu_si128(CONST_M128_CAST(xorBlocks))); @@ -1271,12 +1281,13 @@ size_t AdvancedProcessBlocks128_6x1_ALTIVEC(F1 func1, F6 func6, const word32 *su ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0; ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; + flags = XorBlocksToFlags(xorBlocks, flags); // Coverity hack if (flags & BT_ReverseDirection) { - inBlocks += length - blockSize; - xorBlocks += length - blockSize; - outBlocks += length - blockSize; + inBlocks += static_cast(length) - blockSize; + xorBlocks += static_cast(length) - blockSize; + outBlocks += static_cast(length) - blockSize; inIncrement = 0-inIncrement; xorIncrement = 0-xorIncrement; outIncrement = 0-outIncrement;