blob: 3633e8ee26e0b28018f5eb5624463b3094b0e0c3 [file] [log] [blame]
Anna Zaks568bdee2012-06-09 01:04:54 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
Anna Zaks70186fc2012-06-09 01:05:01 +00005 <title>FAQ and How to Deal with Common False Positives</title>
Anna Zaks568bdee2012-06-09 01:04:54 +00006 <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 Kremenekdea37f22012-06-09 20:10:45 +000010 tr:first-child { width:20%; }
Anna Zaks568bdee2012-06-09 01:04:54 +000011 </style>
12</head>
13<body>
14
15<div id="page">
16<!--#include virtual="menu.html.incl"-->
17
18<div id="content">
19
Ted Kremenekc1cb12b2012-06-09 20:10:42 +000020<h1>FAQ and How to Deal with Common False Positives</h1>
Anna Zaks568bdee2012-06-09 01:04:54 +000021
Anna Zakse06f5a02012-06-11 22:09:44 +000022<ol>
23 <li><a href="#custom_assert">How do I tell the analyzer that I do not want the bug being
24reported here since my custom error handler will safely end the execution before
25the bug is reached?</a></li>
26 <li><a href="#null_pointer">The analyzer reports a null dereference, but I know that the
27pointer is never null. How can I tell the analyzer that a pointer can never be
28null?</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>
31</ol>
32
33
34<h4 id="custom_assert" class="faq">Q: How do I tell the analyzer that I do not want the bug being
Ted Kremenekdea37f22012-06-09 20:10:45 +000035reported here since my custom error handler will safely end the execution before
36the bug is reached?</h4>
Anna Zaks568bdee2012-06-09 01:04:54 +000037
38<img src="images/example_custom_assert.png" alt="example custom assert">
39
Anna Zakse06f5a02012-06-11 22:09:44 +000040<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 Zaks568bdee2012-06-09 01:04:54 +000041
Anna Zakse06f5a02012-06-11 22:09:44 +000042<pre class="code_example">
43void customAssert() <span class="code_highlight">__attribute__((analyzer_noreturn))</span>;
44int foo(int *b) {
45 if (!b)
46 customAssert();
47 return *b;
48}</pre>
49
50
51<h4 id="null_pointer" class="faq">Q: The analyzer reports a null dereference, but I know that the
Ted Kremenekdea37f22012-06-09 20:10:45 +000052pointer is never null. How can I tell the analyzer that a pointer can never be
53null?</h4>
Anna Zaks568bdee2012-06-09 01:04:54 +000054
55<img src="images/example_null_pointer.png" alt="example null pointer">
56
Ted Kremenekdea37f22012-06-09 20:10:45 +000057<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 Zaks568bdee2012-06-09 01:04:54 +000058
Anna Zakse06f5a02012-06-11 22:09:44 +000059<pre class="code_example">
60void usePointer(int *b);
61int foo(int *b) {
62 usePointer(b);
63 return *b;
64}</pre>
65
66<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 Zaks568bdee2012-06-09 01:04:54 +000067
68<img src="images/example_use_assert.png" alt="example use assert">
69
Ted Kremenekdea37f22012-06-09 20:10:45 +000070<p>You can teach the analyzer facts about your code as well as document it by
71using assertions. In the contrived example above, the analyzer reports an error
72on the path which assumes that the loop is never entered. However, the owner of
73the code might know that the loop is always entered because the input parameter
74<tt>length</tt> is always greater than <tt>0</tt>. The false positive can be
75suppressed by asserting this knowledge, adding <tt>assert(length > 0)</tt> in
76the beginning of the function.</p>
Anna Zaks568bdee2012-06-09 01:04:54 +000077
Anna Zakse06f5a02012-06-11 22:09:44 +000078<pre class="code_example">
79int foo(int length) {
80 int x = 0;
81 <span class="code_highlight">assert(length > 0);</span>
82 for (int i = 0; i < length; i++)
83 x += 1;
84 return length/x;
85}
86</pre>
Anna Zaks568bdee2012-06-09 01:04:54 +000087
Anna Zakse06f5a02012-06-11 22:09:44 +000088<h4 id="suppress_issue" class="faq">Q: How can I suppress a specific analyzer warning?</h4>
Anna Zaks568bdee2012-06-09 01:04:54 +000089
Ted Kremenekc1cb12b2012-06-09 20:10:42 +000090<p>There is currently no mechanism for suppressing the analyzer warning,
91although this is currently being investigated. If you encounter an analyzer
92bug/false positive, please <a href = "filing_bugs.html">report it</a>.</p>
Anna Zaks568bdee2012-06-09 01:04:54 +000093
94</div>
95</div>
96</body>
97</html>
98