2007 JavaOneSM Conference | Session TS-2007 |
TS-2007
Improving Software Quality with Static Analysis
William Pugh Professor
- Univ. of Maryland
http://www.cs.umd.edu/~pugh
Improving Software Quality with Static Analysis William Pugh - - PowerPoint PPT Presentation
Improving Software Quality with Static Analysis William Pugh Professor Univ. of Maryland http://www.cs.umd.edu/~pugh TS-2007 2007 JavaOne SM Conference | Session TS-2007 | You will believe... Static analysis tools can find real bugs
2007 JavaOneSM Conference | Session TS-2007 |
TS-2007
William Pugh Professor
http://www.cs.umd.edu/~pugh
2007 JavaOneSM Conference | Session TS-2007 | 2
Static analysis tools can find real bugs and real issues in your code. You can and should effectively incorporate static analysis into your software development process.
2007 JavaOneSM Conference | Session TS-2007 | 3
Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up
2007 JavaOneSM Conference | Session TS-2007 | 4
research in programming languages, algorithms, software engineering
305 (Annotations for Software Defect Detection)
the Java™ Programming Language
2007 JavaOneSM Conference | Session TS-2007 | 5
Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up
2007 JavaOneSM Conference | Session TS-2007 | 6
supposed to do
2007 JavaOneSM Conference | Session TS-2007 | 7
programming, code inspections) for finding bugs early
subtle, and finding them must require sophisticated static analysis techniques
issues
2007 JavaOneSM Conference | Session TS-2007 | 8
if (listeners == null) listeners.remove(listener);
2007 JavaOneSM Conference | Session TS-2007 | 9
parentheses or brackets, etc.)
catches them
error?
2007 JavaOneSM Conference | Session TS-2007 | 10
KlocWork, Coverity, Parasoft, SureLogic
2007 JavaOneSM Conference | Session TS-2007 | 11
2007 JavaOneSM Conference | Session TS-2007 | 12
something the developer did not intend
Selected categories for today's discussion
2007 JavaOneSM Conference | Session TS-2007 | 13
find lots of bugs
2007 JavaOneSM Conference | Session TS-2007 | 14
the return value
Whatever you need to find the bugs
2007 JavaOneSM Conference | Session TS-2007 | 15
look at
newly written module with 1,000 lines of code
code that has been in production for a year
different tools
2007 JavaOneSM Conference | Session TS-2007 | 16
Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up
2007 JavaOneSM Conference | Session TS-2007 | 17
Stuff you really want to look at
category for issues we are most confident are wrong
issues
you should look at when scanning that million line code base
2007 JavaOneSM Conference | Session TS-2007 | 18
with his constructor: /** Construct a WebSpider */ public WebSpider() { WebSpider w = new WebSpider(); }
same bug
... Students are good bug generators
2007 JavaOneSM Conference | Session TS-2007 | 19
public String foundType() {
return this.foundType(); }
2007 JavaOneSM Conference | Session TS-2007 | 20
executed, guarantees a null pointer exception
program contains a statement/branch that can’t be executed
is false, then a NPE will be thrown
2007 JavaOneSM Conference | Session TS-2007 | 21
guarantee NPE
could generate a NPE on valid input
unreachable branches or statements, or reachable only with erroneous input
JDK1.6.0-b105
2007 JavaOneSM Conference | Session TS-2007 | 22
//com.sun.corba.se.impl.naming.cosnaming.NamingContextImpl
if (name != null || name.length > 0)
//com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser
if (part == null | part.equals(""))
// sun.awt.x11.ScrollPanePeer
if (g != null) paintScrollBars(g,colors); g.dispose();
simple ones
2007 JavaOneSM Conference | Session TS-2007 | 23
// java.awt.image.LoopupOp, lines 236-247 public final WritableRaster filter( Raster src, WritableRaster dst) { int dstLength = dst.getNumBands(); // Create a new destination Raster, // if needed if (dst == null) dst = createCompatibleDestRaster(src); Also known as a reverse null dereference error
2007 JavaOneSM Conference | Session TS-2007 | 24
be null because there would have been a NPE if it were null Is it a bug or a redundant check?
2007 JavaOneSM Conference | Session TS-2007 | 25
toLowerCase() return new String
methods
2007 JavaOneSM Conference | Session TS-2007 | 26
// com.sun.rowset.CachedRowSetImpl
if (type == Types.DECIMAL || type == Types.NUMERIC) ((java.math.BigDecimal)x).setScale(scale);
// com.sun.xml.internal.txw2.output.XMLWriter
try { ... } catch (IOException e) { new SAXException("Server side Exception:" + e); }
2007 JavaOneSM Conference | Session TS-2007 | 27
equals(...) is pointless, and almost certainly not what was intended
doesn't generate a compile time error
2007 JavaOneSM Conference | Session TS-2007 | 28
2007 JavaOneSM Conference | Session TS-2007 | 29
nasty code
expected
2007 JavaOneSM Conference | Session TS-2007 | 30
// com.sun.jndi.dns.DnsName, lines 345-347 if (n instanceof CompositeName) { // force ClassCastException n = (DnsName) n; } // sun.jdbc.odbc.JdbcOdbcObject, lines 85-91 if ((b[offset] < 32) || (b[offset] > 128)) { asciiLine += "."; } bad code that does what it was intended to do
2007 JavaOneSM Conference | Session TS-2007 | 31
// com.sun.corba.se.impl.dynamicany.DynAnyComplexImpl String expectedMemberName = null; try { expectedMemberName = expectedTypeCode.member_name(i); } catch (BadKind badKind) { // impossible } catch (Bounds bounds) { // impossible } if ( !(expectedMemberName.equals(memberName) ... )) {
Code that shouldn't go wrong
2007 JavaOneSM Conference | Session TS-2007 | 32
// com.sun.org.apache.xml.internal.security.encryption.XMLCiper // lines 2224-2228 if (null == element) { //complain } String algorithm = element.getAttributeNS(...);
When you are already doomed
2007 JavaOneSM Conference | Session TS-2007 | 33
should be fixed
FindBugs
Evaluating Static Analysis Defect Warnings On Production Software, ACM 2007 Workshop on Program Analysis for Software Tools and Engineering
2007 JavaOneSM Conference | Session TS-2007 | 34
Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up
2007 JavaOneSM Conference | Session TS-2007 | 35
hashCode from Object
same hash code
never want a developer to yawn when I show them a "correctness" bug
2007 JavaOneSM Conference | Session TS-2007 | 36
think your objects will ever get put into a HashMap?
public int hashCode() { assert false : "hashCode method not designed"; return 42; }
2007 JavaOneSM Conference | Session TS-2007 | 37
defined equals but not hashCode as a correctness problem
HashMap/HashTable as a correctness warning
2007 JavaOneSM Conference | Session TS-2007 | 38
Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up
2007 JavaOneSM Conference | Session TS-2007 | 39
application, force your program to things it shouldn't?
2007 JavaOneSM Conference | Session TS-2007 | 40
internal components (e.g., arrays, Date)
internal state
JDK1.6.0-b105
2007 JavaOneSM Conference | Session TS-2007 | 41
Attacks and Avoiding Antipatterns
javax.swing.DefaultListCellRenderer.noFocusBorder
final, because some code might depend upon being able to change it?"
Any untrusted applet can change the static fields
2007 JavaOneSM Conference | Session TS-2007 | 42
worried/paranoid about these issues, you are being irresponsible
2007 JavaOneSM Conference | Session TS-2007 | 43
String query = "SELECT cc_type, cc_number FROM " + "user_data WHERE last_name = '" + user + "'";
statements with constant Strings
build query strings
2007 JavaOneSM Conference | Session TS-2007 | 44
HTML response
clicks on
2007 JavaOneSM Conference | Session TS-2007 | 45
2007 JavaOneSM Conference | Session TS-2007 | 46
values
2007 JavaOneSM Conference | Session TS-2007 | 47
Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up
2007 JavaOneSM Conference | Session TS-2007 | 48
FindBugs and Fortify SCA
2007 JavaOneSM Conference | Session TS-2007 | 49
Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up
2007 JavaOneSM Conference | Session TS-2007 | 50
interested in
reviewing, you decide not to fix
2007 JavaOneSM Conference | Session TS-2007 | 51
2007 JavaOneSM Conference | Session TS-2007 | 52
remember your evaluation when it analyzes that code again?
2007 JavaOneSM Conference | Session TS-2007 | 53
pattern is better
mistake so stupid “no one else could possible make the same mistake”
2007 JavaOneSM Conference | Session TS-2007 | 54
Introduction Correctness issues Bad Practice Security defects Demos (FindBugs, Fortify SCA) Integrating static analysis Wrap up
2007 JavaOneSM Conference | Session TS-2007 | 55
started
your development process
2007 JavaOneSM Conference | Session TS-2007 | 56
analysis tools
are allowed to be null
Types
2007 JavaOneSM Conference | Session TS-2007 | 57
Analysis Tools to Boost Java Code Quality
Dynamic Code Analysis
Klockwork*, Fortify Software*, Coverity*, Parasoft
* - also in exhibit hall
2007 JavaOneSM Conference | Session TS-2007 | 58
William Pugh Professor
http://www.cs.umd.edu/~pugh
2007 JavaOneSM Conference | Session TS-2007 | 59
2007 JavaOneSM Conference | Session TS-2007 | 60
2007 JavaOneSM Conference | Session TS-2007 | 61
quick fixes
2007 JavaOneSM Conference | Session TS-2007 | 62