123 lines
5 KiB
HTML
123 lines
5 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
|
||
|
"http://www.w3.org/TR/html4/strict.dtd">
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>FAQ and How to Deal with Common False Positives</title>
|
||
|
<link type="text/css" rel="stylesheet" href="menu.css">
|
||
|
<link type="text/css" rel="stylesheet" href="content.css">
|
||
|
<script type="text/javascript" src="scripts/menu.js"></script>
|
||
|
<style type="text/css">
|
||
|
tr:first-child { width:20%; }
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
|
||
|
<div id="page">
|
||
|
<!--#include virtual="menu.html.incl"-->
|
||
|
|
||
|
<div id="content">
|
||
|
|
||
|
<h1>FAQ and How to Deal with Common False Positives</h1>
|
||
|
|
||
|
<ol>
|
||
|
<li><a href="#custom_assert">How do I tell the analyzer that I do not want the bug being
|
||
|
reported here since my custom error handler will safely end the execution before
|
||
|
the bug is reached?</a></li>
|
||
|
<li><a href="#null_pointer">The analyzer reports a null dereference, but I know that the
|
||
|
pointer is never null. How can I tell the analyzer that a pointer can never be
|
||
|
null?</a></li>
|
||
|
<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>
|
||
|
<li><a href="#suppress_issue">How can I suppress a specific analyzer warning?</a></li>
|
||
|
<li><a href="#exclude_code">How can I selectively exclude code the analyzer examines?</a></li>
|
||
|
</ol>
|
||
|
|
||
|
|
||
|
<h4 id="custom_assert" class="faq">Q: How do I tell the analyzer that I do not want the bug being
|
||
|
reported here since my custom error handler will safely end the execution before
|
||
|
the bug is reached?</h4>
|
||
|
|
||
|
<img src="images/example_custom_assert.png" alt="example custom assert">
|
||
|
|
||
|
<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>
|
||
|
|
||
|
<pre class="code_example">
|
||
|
void customAssert() <span class="code_highlight">__attribute__((analyzer_noreturn))</span>;
|
||
|
int foo(int *b) {
|
||
|
if (!b)
|
||
|
customAssert();
|
||
|
return *b;
|
||
|
}</pre>
|
||
|
|
||
|
|
||
|
<h4 id="null_pointer" class="faq">Q: The analyzer reports a null dereference, but I know that the
|
||
|
pointer is never null. How can I tell the analyzer that a pointer can never be
|
||
|
null?</h4>
|
||
|
|
||
|
<img src="images/example_null_pointer.png" alt="example null pointer">
|
||
|
|
||
|
<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>
|
||
|
|
||
|
<pre class="code_example">
|
||
|
void usePointer(int *b);
|
||
|
int foo(int *b) {
|
||
|
usePointer(b);
|
||
|
return *b;
|
||
|
}</pre>
|
||
|
|
||
|
<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>
|
||
|
|
||
|
<img src="images/example_use_assert.png" alt="example use assert">
|
||
|
|
||
|
<p> In the contrived example above, the analyzer has detected that the body of
|
||
|
the loop is never entered for the case where <tt>length <= 0</tt>. In this
|
||
|
particular example, you may know that the loop will always be entered because
|
||
|
the input parameter <tt>length</tt> will be greater than zero in all calls to this
|
||
|
function. You can teach the analyzer facts about your code as well as document
|
||
|
it by using assertions. By adding <tt>assert(length > 0)</tt> in the beginning
|
||
|
of the function, you tell the analyzer that your code is never expecting a zero
|
||
|
or a negative value, so it won't need to test the correctness of those paths.
|
||
|
</p>
|
||
|
|
||
|
<pre class="code_example">
|
||
|
int foo(int length) {
|
||
|
int x = 0;
|
||
|
<span class="code_highlight">assert(length > 0);</span>
|
||
|
for (int i = 0; i < length; i++)
|
||
|
x += 1;
|
||
|
return length/x;
|
||
|
}
|
||
|
</pre>
|
||
|
|
||
|
<h4 id="suppress_issue" class="faq">Q: How can I suppress a specific analyzer warning?</h4>
|
||
|
|
||
|
<p>There is currently no solid mechanism for suppressing an analyzer warning,
|
||
|
although this is currently being investigated. When you encounter an analyzer
|
||
|
bug/false positive, check if it's one of the issues discussed above or if the
|
||
|
analyzer <a href = "annotations.html#custom_assertions" >annotations</a> can
|
||
|
resolve the issue. Second, please <a href = "filing_bugs.html">report it</a> to
|
||
|
help us improve user experience. As the last resort, consider using <tt>__clang_analyzer__</tt> macro
|
||
|
<a href = "faq.html#exclude_code" >described below</a>.</p>
|
||
|
|
||
|
<h4 id="exclude_code" class="faq">Q: How can I selectively exclude code the analyzer examines?</h4>
|
||
|
|
||
|
<p>When the static analyzer is using clang to parse source files, it implicitly
|
||
|
defines the preprocessor macro <tt>__clang_analyzer__</tt>. One can use this
|
||
|
macro to selectively exclude code the analyzer examines. Here is an example:
|
||
|
|
||
|
<pre class="code_example">
|
||
|
#ifndef __clang_analyzer__
|
||
|
// Code not to be analyzed
|
||
|
#endif
|
||
|
</pre>
|
||
|
|
||
|
This usage is discouraged because it makes the code dead to the analyzer from
|
||
|
now on. Instead, we prefer that users file bugs against the analyzer when it flags
|
||
|
false positives.
|
||
|
</p>
|
||
|
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|
||
|
|