2010-12-28 17:41:56 +01:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<article version="5.0" xmlns="http://docbook.org/ns/docbook"
|
|
|
|
xmlns:xlink="http://www.w3.org/1999/xlink"
|
|
|
|
xmlns:xi="http://www.w3.org/2001/XInclude"
|
|
|
|
xmlns:svg="http://www.w3.org/2000/svg"
|
|
|
|
xmlns:m="http://www.w3.org/1998/Math/MathML"
|
|
|
|
xmlns:html="http://www.w3.org/1999/xhtml"
|
|
|
|
xmlns:db="http://docbook.org/ns/docbook">
|
|
|
|
<info>
|
2010-12-28 19:47:56 +01:00
|
|
|
<title>Cppcheck Design</title>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
|
|
|
<author>
|
|
|
|
<personname><firstname>Daniel</firstname><surname>Marjamäki</surname></personname>
|
|
|
|
|
|
|
|
<affiliation>
|
|
|
|
<orgname>Cppcheck</orgname>
|
|
|
|
</affiliation>
|
|
|
|
</author>
|
|
|
|
|
|
|
|
<pubdate>2010</pubdate>
|
|
|
|
</info>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Introduction</title>
|
|
|
|
|
2010-12-29 18:03:57 +01:00
|
|
|
<para>The goal with this article is to give users an idea of how Cppcheck
|
|
|
|
works.</para>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
2010-12-29 18:03:57 +01:00
|
|
|
<para>Cppcheck is a static analysis tool that tries to completely avoid
|
|
|
|
false warnings. A false warning is when the tool reports that there is an
|
|
|
|
error even though there is no error.</para>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
2010-12-29 18:03:57 +01:00
|
|
|
<para>Cppcheck is a relatively simple tool. I hope that this article will
|
|
|
|
highlight that it is possible to avoid false warnings with simple
|
|
|
|
analysis.</para>
|
2010-12-28 17:41:56 +01:00
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Limitations of static analysis</title>
|
|
|
|
|
2010-12-28 19:47:56 +01:00
|
|
|
<para>There are many bugs in programs that are really hard to detect for
|
|
|
|
tools. Here is an example:</para>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
2010-12-28 19:47:56 +01:00
|
|
|
<programlisting>// calculate the number of days
|
2010-12-28 17:41:56 +01:00
|
|
|
int days(int hours)
|
|
|
|
{
|
|
|
|
return hours / 23;
|
|
|
|
}</programlisting>
|
|
|
|
|
2010-12-28 19:47:56 +01:00
|
|
|
<para>A human programmer knows that there are 24 hours in a day and
|
|
|
|
therefore he could see that "23" is wrong. A tool will probably not know
|
2010-12-28 17:41:56 +01:00
|
|
|
that there are 24 hours in a day.</para>
|
|
|
|
|
2010-12-29 18:03:57 +01:00
|
|
|
<para>A tool that tries to detect all bugs could write a warning message
|
|
|
|
for every calculation in the program. Then it will correctly report that
|
|
|
|
"hours / 23" is wrong but incorrectly warn about "hours / 24".</para>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
|
|
|
<para>Cppcheck will only write a warning message if it can determine that
|
2010-12-28 19:47:56 +01:00
|
|
|
the calculation is wrong. In this case, no error will be written.</para>
|
2010-12-28 17:41:56 +01:00
|
|
|
</section>
|
|
|
|
|
2010-12-29 18:03:57 +01:00
|
|
|
<section>
|
|
|
|
<title>Control flow analysis</title>
|
|
|
|
|
|
|
|
<para>Control flow analysis is when the tool tries to determine if certain
|
|
|
|
execution paths are possible.</para>
|
|
|
|
|
|
|
|
<programlisting>void f(int x)
|
|
|
|
{
|
|
|
|
if (x == 1)
|
|
|
|
f1();
|
|
|
|
if (x & 2)
|
|
|
|
f2();
|
|
|
|
}</programlisting>
|
|
|
|
|
|
|
|
<para>The function has 3 possible execution paths. The analysis you do in
|
|
|
|
your head when you determine that there are 3 possible execution paths is
|
|
|
|
"control flow analysis".</para>
|
|
|
|
|
|
|
|
<para>When you review code you will probably use "control flow analysis"
|
|
|
|
in your head to determine if there are bugs or not.</para>
|
|
|
|
|
|
|
|
<para>The control flow analysis in Cppcheck is quite simple.</para>
|
|
|
|
</section>
|
|
|
|
|
2010-12-28 17:41:56 +01:00
|
|
|
<section>
|
|
|
|
<title>Buffer overflows</title>
|
|
|
|
|
2010-12-28 19:47:56 +01:00
|
|
|
<para>This is a simple description of how buffer overflows are detected by
|
2010-12-28 17:41:56 +01:00
|
|
|
Cppcheck.</para>
|
|
|
|
|
2010-12-28 19:47:56 +01:00
|
|
|
<para>For simple cases, no control flow analysis is used. If an array is
|
|
|
|
accessed out of bounds somewhere in its scope then an error message will
|
|
|
|
be written.</para>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
|
|
|
<para>An example code:</para>
|
|
|
|
|
|
|
|
<programlisting>void f()
|
|
|
|
{
|
|
|
|
char a[10];
|
|
|
|
if (x + y == 2) {
|
|
|
|
a[20] = 0;
|
|
|
|
}
|
|
|
|
}</programlisting>
|
|
|
|
|
|
|
|
<para>Cppcheck will report this message:</para>
|
|
|
|
|
|
|
|
<programlisting>Array 'a[10]' index 20 out of bounds</programlisting>
|
|
|
|
|
|
|
|
<para>Cppcheck will not try to determine how execution can reach the
|
|
|
|
"a[20] = 0;" statement. It is assumed that all statements are reachable.
|
|
|
|
Cppcheck will detect the error even if it is really impossible that "x + y
|
|
|
|
== 2" is true. I still claim that this is a correct warning because the
|
|
|
|
statement is there and it has the error.</para>
|
|
|
|
|
|
|
|
<para>Cppcheck will also investigate function calls. But then control flow
|
2010-12-28 19:47:56 +01:00
|
|
|
analysis is needed to avoid false warnings. Here is an example that
|
|
|
|
logically is the same as the previous example:</para>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
|
|
|
<para><programlisting>void f1(char *s)
|
|
|
|
{
|
|
|
|
s[20] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void f2()
|
|
|
|
{
|
|
|
|
char a[10];
|
|
|
|
if (x + y == 2) {
|
|
|
|
f1(a);
|
|
|
|
}
|
|
|
|
}</programlisting>Cppcheck will report this message:</para>
|
|
|
|
|
|
|
|
<programlisting>Array 'a[10]' index 20 out of bounds</programlisting>
|
|
|
|
|
|
|
|
<para>If the execution reaches the function call then there will be an
|
|
|
|
error.</para>
|
|
|
|
|
|
|
|
<para>But if the condition is moved into "f1" then it will be necessary to
|
|
|
|
prove that "x+y==2" can be true when the function is called from
|
|
|
|
"f2".</para>
|
|
|
|
|
|
|
|
<para>No error message is reported for this code:</para>
|
|
|
|
|
|
|
|
<para><programlisting>void f1(char *s)
|
|
|
|
{
|
|
|
|
if (x + y == 2) {
|
|
|
|
s[20] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f2()
|
|
|
|
{
|
|
|
|
char a[10];
|
2010-12-28 19:47:56 +01:00
|
|
|
f1(a);
|
2010-12-28 17:41:56 +01:00
|
|
|
}</programlisting></para>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<title>Memory leaks</title>
|
|
|
|
|
|
|
|
<para>Simple control-flow analysis is made. The assumtion is that all
|
2010-12-28 19:47:56 +01:00
|
|
|
conditions can always be either true or false. It is assumed that all
|
|
|
|
statements are reachable.</para>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
|
|
|
<para>Here is an example:</para>
|
|
|
|
|
|
|
|
<programlisting>void f()
|
|
|
|
{
|
|
|
|
char *a = malloc(10);
|
|
|
|
if (x + y == 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(a);
|
|
|
|
}</programlisting>
|
|
|
|
|
|
|
|
<para>Cppcheck will determine that there is a leak at the "return;"
|
|
|
|
statement:</para>
|
|
|
|
|
|
|
|
<programlisting>Memory leak: a</programlisting>
|
|
|
|
|
|
|
|
<para>Cppcheck doesn't try to determine how the execution reaches the
|
|
|
|
"return;" statement. It will only see that if the execution reaches the
|
|
|
|
"return;" then there will be a memory leak.</para>
|
|
|
|
|
2010-12-28 19:47:56 +01:00
|
|
|
<para>Lack of advanced control-flow analysis means that many bugs are not
|
|
|
|
detected:</para>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
|
|
|
<programlisting>void f(int x)
|
|
|
|
{
|
|
|
|
char *a = 0;
|
|
|
|
|
|
|
|
if (x == 10)
|
|
|
|
a = malloc(10);
|
|
|
|
|
|
|
|
if (x == 20)
|
|
|
|
free(a);
|
|
|
|
}</programlisting>
|
|
|
|
|
2010-12-28 19:47:56 +01:00
|
|
|
<para>Cppcheck doesn't detect any error. The "all conditions can be either
|
|
|
|
true/false" means that cppcheck doesn't know that "if (x==20)" is false
|
|
|
|
when "if (x==10)" is true. Many other static analysis tools will probably
|
|
|
|
detect that there will be a leak if x is 10.</para>
|
|
|
|
</section>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
2010-12-28 19:47:56 +01:00
|
|
|
<section>
|
|
|
|
<title>Final thoughts</title>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
2010-12-28 19:47:56 +01:00
|
|
|
<para>You can not trust that Cppcheck will detect all bugs.</para>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
2010-12-28 19:47:56 +01:00
|
|
|
<para>Cppcheck will just find some bugs. It is likely that you won't find
|
|
|
|
these bugs unless you use Cppcheck. Cppcheck has found bugs in production
|
|
|
|
code that has been used for years.</para>
|
2010-12-28 17:41:56 +01:00
|
|
|
|
2010-12-28 19:47:56 +01:00
|
|
|
<para></para>
|
2010-12-28 17:41:56 +01:00
|
|
|
</section>
|
|
|
|
</article>
|