Skip to content

Commit d9fb7af

Browse files
filbrandenkeszybz
authored andcommitted
coverity: Add custom assertion macros for Coverity
These custom macros make the expression go through a function, in order to prevent ASSERT_SIDE_EFFECT false positives on our macros such as assert_se() and assert_return() that cannot be disabled and will always evaluate their expressions. This technique has been described and recommended in: https://community.synopsys.com/s/question/0D534000046Yuzb/suppressing-assertsideeffect-for-functions-that-allow-for-sideeffects Tested by doing a local cov-build and uploading the resulting tarball to scan.coverity.com, confirmed that the ASSERT_SIDE_EFFECT false positives were gone.
1 parent 6a8b230 commit d9fb7af

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/basic/macro.h

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,48 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
247247
(__x / __y + !!(__x % __y)); \
248248
})
249249

250+
#ifdef __COVERITY__
251+
252+
/* Use special definitions of assertion macros in order to prevent
253+
* false positives of ASSERT_SIDE_EFFECT on Coverity static analyzer
254+
* for uses of assert_se() and assert_return().
255+
*
256+
* These definitions make expression go through a (trivial) function
257+
* call to ensure they are not discarded. Also use ! or !! to ensure
258+
* the boolean expressions are seen as such.
259+
*
260+
* This technique has been described and recommended in:
261+
* https://community.synopsys.com/s/question/0D534000046Yuzb/suppressing-assertsideeffect-for-functions-that-allow-for-sideeffects
262+
*/
263+
264+
extern void __coverity_panic__(void);
265+
266+
static inline int __coverity_check__(int condition) {
267+
return condition;
268+
}
269+
270+
#define assert_message_se(expr, message) \
271+
do { \
272+
if (__coverity_check__(!(expr))) \
273+
__coverity_panic__(); \
274+
} while (false)
275+
276+
#define assert_log(expr, message) __coverity_check__(!!(expr))
277+
278+
#else /* ! __COVERITY__ */
279+
250280
#define assert_message_se(expr, message) \
251281
do { \
252282
if (_unlikely_(!(expr))) \
253283
log_assert_failed(message, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
254284
} while (false)
255285

286+
#define assert_log(expr, message) ((_likely_(expr)) \
287+
? (true) \
288+
: (log_assert_failed_return(message, __FILE__, __LINE__, __PRETTY_FUNCTION__), false))
289+
290+
#endif /* __COVERITY__ */
291+
256292
#define assert_se(expr) assert_message_se(expr, #expr)
257293

258294
/* We override the glibc assert() here. */
@@ -285,10 +321,6 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
285321
REENABLE_WARNING
286322
#endif
287323

288-
#define assert_log(expr, message) ((_likely_(expr)) \
289-
? (true) \
290-
: (log_assert_failed_return(message, __FILE__, __LINE__, __PRETTY_FUNCTION__), false))
291-
292324
#define assert_return(expr, r) \
293325
do { \
294326
if (!assert_log(expr, #expr)) \

0 commit comments

Comments
 (0)