Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
Anna Zaks | 70186fc | 2012-06-09 01:05:01 +0000 | [diff] [blame] | 5 | <title>FAQ and How to Deal with Common False Positives</title> |
Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 6 | <link type="text/css" rel="stylesheet" href="menu.css"> |
| 7 | <link type="text/css" rel="stylesheet" href="content.css"> |
| 8 | <script type="text/javascript" src="scripts/menu.js"></script> |
| 9 | <style type="text/css"> |
Ted Kremenek | dea37f2 | 2012-06-09 20:10:45 +0000 | [diff] [blame] | 10 | tr:first-child { width:20%; } |
Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 11 | </style> |
| 12 | </head> |
| 13 | <body> |
| 14 | |
| 15 | <div id="page"> |
| 16 | <!--#include virtual="menu.html.incl"--> |
| 17 | |
| 18 | <div id="content"> |
| 19 | |
Ted Kremenek | c1cb12b | 2012-06-09 20:10:42 +0000 | [diff] [blame] | 20 | <h1>FAQ and How to Deal with Common False Positives</h1> |
Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 21 | |
Anna Zaks | e06f5a0 | 2012-06-11 22:09:44 +0000 | [diff] [blame] | 22 | <ol> |
| 23 | <li><a href="#custom_assert">How do I tell the analyzer that I do not want the bug being |
| 24 | reported here since my custom error handler will safely end the execution before |
| 25 | the bug is reached?</a></li> |
| 26 | <li><a href="#null_pointer">The analyzer reports a null dereference, but I know that the |
| 27 | pointer is never null. How can I tell the analyzer that a pointer can never be |
| 28 | null?</a></li> |
| 29 | <li><a href="#use_assert">The analyzer assumes that a loop body is never entered. How can I tell it that the loop body will be entered at least once?</a></li> |
| 30 | <li><a href="#suppress_issue">How can I suppress a specific analyzer warning?</a></li> |
Anna Zaks | 843f0e8 | 2012-06-16 00:30:21 +0000 | [diff] [blame] | 31 | <li><a href="#exclude_code">How can I selectively exclude code the analyzer examines?</a></li> |
Anna Zaks | e06f5a0 | 2012-06-11 22:09:44 +0000 | [diff] [blame] | 32 | </ol> |
| 33 | |
| 34 | |
| 35 | <h4 id="custom_assert" class="faq">Q: How do I tell the analyzer that I do not want the bug being |
Ted Kremenek | dea37f2 | 2012-06-09 20:10:45 +0000 | [diff] [blame] | 36 | reported here since my custom error handler will safely end the execution before |
| 37 | the bug is reached?</h4> |
Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 38 | |
| 39 | <img src="images/example_custom_assert.png" alt="example custom assert"> |
| 40 | |
Anna Zaks | e06f5a0 | 2012-06-11 22:09:44 +0000 | [diff] [blame] | 41 | <p>You can tell the analyzer that this path is unreachable by teaching it about your <a href = "annotations.html#custom_assertions" >custom assertion handlers</a>. For example, you can modify the code segment as following.</p> |
Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 42 | |
Anna Zaks | e06f5a0 | 2012-06-11 22:09:44 +0000 | [diff] [blame] | 43 | <pre class="code_example"> |
| 44 | void customAssert() <span class="code_highlight">__attribute__((analyzer_noreturn))</span>; |
| 45 | int foo(int *b) { |
| 46 | if (!b) |
| 47 | customAssert(); |
| 48 | return *b; |
| 49 | }</pre> |
| 50 | |
| 51 | |
| 52 | <h4 id="null_pointer" class="faq">Q: The analyzer reports a null dereference, but I know that the |
Ted Kremenek | dea37f2 | 2012-06-09 20:10:45 +0000 | [diff] [blame] | 53 | pointer is never null. How can I tell the analyzer that a pointer can never be |
| 54 | null?</h4> |
Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 55 | |
| 56 | <img src="images/example_null_pointer.png" alt="example null pointer"> |
| 57 | |
Ted Kremenek | dea37f2 | 2012-06-09 20:10:45 +0000 | [diff] [blame] | 58 | <p>The reason the analyzer often thinks that a pointer can be null is because the preceding code checked compared it against null. So if you are absolutely sure that it cannot be null, remove the preceding check and, preferably, add an assertion as well. For example, in the code segment above, it will be sufficient to remove the <tt>if (!b)</tt> check. </p> |
Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 59 | |
Anna Zaks | e06f5a0 | 2012-06-11 22:09:44 +0000 | [diff] [blame] | 60 | <pre class="code_example"> |
| 61 | void usePointer(int *b); |
| 62 | int foo(int *b) { |
| 63 | usePointer(b); |
| 64 | return *b; |
| 65 | }</pre> |
| 66 | |
| 67 | <h4 id="use_assert" class="faq">Q: The analyzer assumes that a loop body is never entered. How can I tell it that the loop body will be entered at least once?</h4> |
Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 68 | |
| 69 | <img src="images/example_use_assert.png" alt="example use assert"> |
| 70 | |
Ted Kremenek | dea37f2 | 2012-06-09 20:10:45 +0000 | [diff] [blame] | 71 | <p>You can teach the analyzer facts about your code as well as document it by |
| 72 | using assertions. In the contrived example above, the analyzer reports an error |
| 73 | on the path which assumes that the loop is never entered. However, the owner of |
| 74 | the code might know that the loop is always entered because the input parameter |
| 75 | <tt>length</tt> is always greater than <tt>0</tt>. The false positive can be |
| 76 | suppressed by asserting this knowledge, adding <tt>assert(length > 0)</tt> in |
| 77 | the beginning of the function.</p> |
Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 78 | |
Anna Zaks | e06f5a0 | 2012-06-11 22:09:44 +0000 | [diff] [blame] | 79 | <pre class="code_example"> |
| 80 | int foo(int length) { |
| 81 | int x = 0; |
| 82 | <span class="code_highlight">assert(length > 0);</span> |
| 83 | for (int i = 0; i < length; i++) |
| 84 | x += 1; |
| 85 | return length/x; |
| 86 | } |
| 87 | </pre> |
Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 88 | |
Anna Zaks | e06f5a0 | 2012-06-11 22:09:44 +0000 | [diff] [blame] | 89 | <h4 id="suppress_issue" class="faq">Q: How can I suppress a specific analyzer warning?</h4> |
Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 90 | |
Anna Zaks | 843f0e8 | 2012-06-16 00:30:21 +0000 | [diff] [blame] | 91 | <p>There is currently no solid mechanism for suppressing an analyzer warning, |
| 92 | although this is currently being investigated. When you encounter an analyzer |
| 93 | bug/false positive, check if it's one of the issues discussed above or if the |
| 94 | analyzer <a href = "annotations.html#custom_assertions" >annotations</a> can |
| 95 | resolve the issue. Second, please <a href = "filing_bugs.html">report it</a> to |
| 96 | help us improve user experience. As the last resort, consider using <tt>__clang_analyzer__</tt> macro |
| 97 | <a href = "faq.html#exclude_code" >described below</a>.</p> |
| 98 | |
| 99 | <h4 id="exclude_code" class="faq">Q: How can I selectively exclude code the analyzer examines?</h4> |
| 100 | |
| 101 | <p>When the static analyzer is using clang to parse source files, it implicitly |
| 102 | defines the preprocessor macro <tt>__clang_analyzer__</tt>. One can use this |
| 103 | macro to selectively exclude code the analyzer examines. Here is an example: |
| 104 | |
| 105 | <pre class="code_example"> |
| 106 | #ifndef __clang_analyzer__ |
| 107 | // Code not to be analyzed |
| 108 | #endif |
| 109 | </pre> |
| 110 | |
| 111 | This usage is discouraged because it makes the code dead to the analyzer from |
| 112 | now on. Instead, we prefer that users file bugs against the analyzer when it flags |
| 113 | false positives. |
| 114 | </p> |
Anna Zaks | 568bdee | 2012-06-09 01:04:54 +0000 | [diff] [blame] | 115 | |
| 116 | </div> |
| 117 | </div> |
| 118 | </body> |
| 119 | </html> |
| 120 | |