Coverity Prevent: how to handle "checked return" warning when I deliberately don't check the return?
Asked Answered
N

1

6

As the title suggest, for example, in 85% of the situation, I'd like to check the return code of foo(), but sometimes I really don't care about it, but this will raise Coverity warning.

What's the best way to deal with this problem? Changing Coverity settings doesn't count. :)

Nosegay answered 22/2, 2011 at 1:33 Comment(0)
C
8

The correct way to suppress CHECKED_RETURN defect is to cast the return value you don't care about to a void. This has the additional advantage of making it clear to anyone reading the code that you don't care about return value, rather than that you forgot to check it.

Cleave answered 12/11, 2011 at 20:18 Comment(3)
in addition, trying to trick analysis with macros will not work since Coverity is not analyzing the source code but rather the AST.Cleave
Coverity still flagged (void)fread(..) with this error for me after adding the cast to void. Perhaps the option doesn't work for syscalls/functions flagged 'always check'. It was legitimate though, a separate ferror() was covering the failure cases.Absonant
right, statistical checkers honor (void) but ones that are explicitly asked to be checked will flag failure to check even with attempt to suppress. There is always comment annotations to suppress those when needed :)Cleave

© 2022 - 2024 — McMap. All rights reserved.