blob: 94e4d279238e9d22024367a87cdaabf1dfa42b44 [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
Ted Kremenekdea37f22012-06-09 20:10:45 +000022<h4 class="faq">Q: How do I tell the analyzer that I do not want the bug being
23reported here since my custom error handler will safely end the execution before
24the bug is reached?</h4>
Anna Zaks568bdee2012-06-09 01:04:54 +000025
26<img src="images/example_custom_assert.png" alt="example custom assert">
27
28<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>.</p>
29
Ted Kremenekdea37f22012-06-09 20:10:45 +000030<h4 class="faq">Q: The analyzer reports a null dereference, but I know that the
31pointer is never null. How can I tell the analyzer that a pointer can never be
32null?</h4>
Anna Zaks568bdee2012-06-09 01:04:54 +000033
34<img src="images/example_null_pointer.png" alt="example null pointer">
35
Ted Kremenekdea37f22012-06-09 20:10:45 +000036<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 +000037
Ted Kremenekdea37f22012-06-09 20:10:45 +000038<h4 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 +000039
40<img src="images/example_use_assert.png" alt="example use assert">
41
Ted Kremenekdea37f22012-06-09 20:10:45 +000042<p>You can teach the analyzer facts about your code as well as document it by
43using assertions. In the contrived example above, the analyzer reports an error
44on the path which assumes that the loop is never entered. However, the owner of
45the code might know that the loop is always entered because the input parameter
46<tt>length</tt> is always greater than <tt>0</tt>. The false positive can be
47suppressed by asserting this knowledge, adding <tt>assert(length > 0)</tt> in
48the beginning of the function.</p>
Anna Zaks568bdee2012-06-09 01:04:54 +000049
Ted Kremenekdea37f22012-06-09 20:10:45 +000050<h4 class="faq">Q: How can I suppress a specific analyzer warning?</h4>
Anna Zaks568bdee2012-06-09 01:04:54 +000051
52<img src="images/example_null_pointer.png" alt="example null pointer">
53
Ted Kremenekc1cb12b2012-06-09 20:10:42 +000054<p>There is currently no mechanism for suppressing the analyzer warning,
55although this is currently being investigated. If you encounter an analyzer
56bug/false positive, please <a href = "filing_bugs.html">report it</a>.</p>
Anna Zaks568bdee2012-06-09 01:04:54 +000057
58</div>
59</div>
60</body>
61</html>
62