Static Code Analysis
Static Code Analysis
W
ool has a tendency to collect static (perhaps even training) to do the work right.
electricity and thus to attract dust It’s therefore impossible to use them on a pro-
and lint. Developers know that pro- ject’s complete code base.
grams have a similar tendency to Moreover, the earlier we find bugs, the eas-
attract defects and, furthermore, ier it is to fix them. Ideally, we would like to
that many of them aren’t visible to catch errors when we make them, or as close
compilers. In the 1970s, Stephen Johnson, then afterward as possible, and not ipso facto with
at the Bell Laboratories, wrote Lint, a tool to reviewing or testing.
examine C source programs Most errors fall into known categories, as
that had compiled without er- people tend to fall into the same traps repeat-
rors and to find bugs that had edly. It’s exactly the predictability of people’s
escaped detection.1 fallibility that gives tools such as Lint a chance.
There are many ways to re- Lint worked by looking for known error pat-
duce the number of bugs in a terns. It didn’t try to execute the program and
program. Writing tests is one, compare actual with expected behavior, which
and tools such as JUnit help is the dynamic analysis that we do when we
programmers do this.2 Re- test. Instead, Lint trawled through the program
search tells us that code re- source trying to match patterns. Such tools are
views are probably the best called static checkers. They check our programs
way to eliminate bugs. Unfortunately, getting for errors without executing them, in a process
the right people together to study programs called static code analysis.
and identify problem areas in them takes a lot Programmers usually employ static checkers
of time. Code review teams also need practice after compilation and before testing. In this
way, they work with a program that has an ini-
tial indication of correctness (because it com-
piles) and try to avoid well-known traps and
Editor’s introduction pitfalls before measuring it against its specifica-
Bugs are disturbing. This holds in summer with the insect types and tions (when it’s tested). Static checking is rela-
year-round with the software types. While developers have long de- tively painless, although it can be humbling—
ployed reviews, inspections, and different testing strategies to find especially the first time. Lint’s success has given
software bugs, they don’t yet widely use the available semiautomatic today’s programmers many descendant tools,
defect-detection techniques. Panagiotis Louridas explains here how static both open source and proprietary, that target
code-analysis tools are used and what defects they can detect. As usual, different languages and operating systems.
the column compares several open source tools together with a commer-
cially available tool. They can help you reduce the number of bugs of all Static code checking in Java
the software types—security, memory, data typing, and so on—before To see static code checking in action, let’s
you deploy the more expensive verification techniques. start with the coding horror in figure 1. Al-
—Christof Ebert though this program compiles, it fails to do
what the programmer wants. The programmer
intends to read a string from the user, substi-
58 IEEE SOFTWARE Published by the IEEE Computer Society 0740-7459/06/$20.00 © 2006 IEEE
Authorized licensed use limited to: University of Groningen. Downloaded on May 14,2025 at 11:41:12 UTC from IEEE Xplore. Restrictions apply.
OPEN SOURCE
1 import java.io.InputStreamReader;
2 import java.io.BufferedReader;
3 import java.io.IOException;
4
5 public class CodingHorror {
6
7 public static void main(String args[]) {
8
9 InputStreamReader isr = new InputStreamReader(System.in);
10 BufferedReader br = new BufferedReader(isr);
11 String input = null;
12 try {
13 input = br.readLine(); // e.g., peel
14 } catch (IOException ioex) {
15 System.err.println(ioex.getMessage());
16 }
17 input.replace(‘e’, ‘o’);
18 if (input == “pool”) {
19 System.out.println(“User entered peel.”);
20 } else {
21 System.out.println(“User entered something else.”);
22 }
23 }
24 }
tute all “e” characters with “o” char- ■ Because strings are immutable in new string with the results of any re-
acters, then check whether the substi- Java, replace doesn’t modify the placements it carries out. The pro-
tution results in the string “pool.” This original string. Instead, it returns a gram simply ignores the result string
will never be the case, no matter what
input we provide.
To see why, we can use FindBugs
(http://findbugs.sourceforge.net), a pop-
ular open source static code checker for
Java, developed at the University of
Maryland. After running FindBugs on
the program in figure 1, we get the screen
in figure 2. It shows three possible bugs:
60 IEEE SOFTWARE w w w . c o m p u t e r. o r g / s o f t w a r e
Authorized licensed use limited to: University of Groningen. Downloaded on May 14,2025 at 11:41:12 UTC from IEEE Xplore. Restrictions apply.
OPEN SOURCE
Table 1
Static code checkers
Features FindBugs Checkstyle PMD Klocwork K7
Version 0.9.7 4.1 3.6 7.0.4.15
Works on Bytecode Source Source Bytecode and source
Languages Java Java Java Java, C++
Interface GUI, command line, plugin Command line, plugin Command line, plugin GUI, command line, plugin
Detects security vulnerabilities Few No Few Many
Stack overflow analysis No No No Yes (C++)
Custom checkers Yes Yes Yes Yes (C++)
Architectural analysis No No No Yes
Metrics No Few No Many
Web-based project management HTML reports HTML reports HTML reports Yes
Size 3.6 Mbytes 6.8 Mbytes 49.1 Mbytes (of which Depends on the configuration;
48.6 Mbytes is about 250 Mbytes for a
documentation) comprehensive Java installation
License GNU Lesser General Public GNU LGPL Berkeley Software Proprietary
License (LGPL) Distribution-style license
A bunch of code checkers at finding bugs for any given project. IEEE
Besides FindBugs, Checkstyle (http:// Table 1 compares four tools, but it’s
checkstyle.sourceforge.net) and PMD only a starting point.
(http://pmd.sourceforge.net/) are also
P
popular open source tools for Java. rogramming is arguably one of the
Checkstyle started as a tool for check- toughest jobs in project develop-
ing compliance with coding standards, ment. No machine can substitute
but it has evolved considerably and can for good sense, a solid knowledge of
now check for many coding problems. the fundamentals, clear thinking, and
PMD is similar to FindBugs, so it might discipline, but bug detection tools can FUTURE TOPICS:
be useful to try both and see what best help developers. In a recent article,5
Visit us
fits a particular project. Niklaus Wirth opined that “never do
The Klocwork K7 (www.klocwork. programs contain so few bugs as when
The Business of
com) suite is a proprietary solution no debugging tools are available.” Software Engineering
that works on bytecode to find defects
and security vulnerabilities and on
Caveat emptor.
onInspections
Software the
Web
References
web
source code to perform metrics and ar-
chitectural analysis. It therefore lets de- 1. S. Johnson, Lint: A C Program Checker, tech.
report 65, Bell Laboratories, Dec.1977.
Usability
velopers spot different kinds of prob- 2. P. Louridas, “JUnit: Unit Testing and Coding
lems at different detail levels. A license
for five seats offering full functionality
in Tandem,” IEEE Software, vol. 22, no. 4,
2005, pp. 12–15.
Internationalization
3. T.J. McCabe, “A Complexity Measure,” IEEE
for projects up to a half-million lines of Trans. Software Eng., vol. 2, no. 4, 1976, pp.
code costs US$19,975. 308–320.
All static code checkers I’ve exam- 4 S.R. Chidamber and C.F. Kemerer, “A Metrics
Suite for Object Oriented Design,” IEEE
ined let users filter the messages and Trans. Software Eng., vol. 20, no. 6, 1994,
warnings at different levels of sophisti- pp. 476–493.
cation. This feature is important be- 5. N. Wirth, “Good Ideas, through the Looking
Glass,” Computer, vol. 39, no. 1, 2006, pp.
cause false positives can be a big prob- 28–39.
lem; you need a way to reduce the
noise a wealth of warnings produces. Panagiotis Louridas is a grid software engineer at
the Greek Research and Technology Network and a researcher
Only comprehensive testing of the
at the Eltrun Software Engineering and Security research group www.computer.org/software
tools will give developers a clear idea of the Athens University of Economics and Business. Contact him
of how to optimize their potential at louridas@{grnet, aueb}.gr.