`
cywhoyi
  • 浏览: 421252 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Using FindBugs to produce substantially less buggy code

 
阅读更多

Some time ago a user of Java-monitor, the forum of our JCG partner, Kees Jan, spotted that his system was forcing a large number of full Garbage Collections despite the fact that the overall memory utilization was low. A rough estimation for the cause of the problem suggested a potential call to System.gc() performed by one of the libraries in use. Lets see what our partner has to say about clarifying similar issues in order to produce substantially less buggy code.

 

… It did occur to me that there is a tool that could have helped with the System.gc()issue. It is aptly named FindBugs. Actually, there are several (PMD and CheckStylebeing other, similar tools), but FindBugs is nice because you don’t actually need the application’s source code to check it for bugs. All you need are the JAR files and/or class files. Another great feature is that each report is documented very well. You don’t just get vague warnings that you sort-of give up on after a few minutes, but you can find an explanation for each warning on-line.

Here is a sample output of FindBugs, when you would run it against Java-monitor’s probe.

01 $ findbugs -textui -effort:max java-monitor-probes/build/WEB-INF/classes
02 The following classes needed for analysis were missing:
03   javax.servlet.http.HttpServlet
04   javax.servlet.Filter
05   javax.servlet.http.HttpServletResponse
06   javax.servlet.ServletException
07   javax.servlet.ServletConfig
08   javax.servlet.FilterConfig
09   javax.servlet.ServletRequest
10   javax.servlet.ServletResponse
11   javax.servlet.FilterChain
12 Missing classes: 7

From this report you can see that FindBugs cannot find any issues with the code itself. There are some classes that the code depends on. These were not included in the analysis. Since these are all Sun-provided classes I presume them to be bugfree. *cough*

Anyway. Let’s make the analysis a bit more juicy. Here is the output when running FindBugs against the well-known MySQL JDBC driver. I’m sure that many of you use it in production today. This code produces significantly more results and FindBugs takes a while to analyse the whole package. I have shown only a few lines of the 154 warnings.

01 $ findbugs -textui -effort:max mysql-connector-java-5.1.5-bin.jar
02   ......
03 H C NP: Method call in com.mysql.jdbc.profiler.ProfilerEvent.pack() passes null for unconditionally dereferenced parameter of writeBytes(byte[], byte[], int)  Method invoked at ProfilerEvent.java:[line 375]
04   ......
05 M D NP: Possible null pointer dereference of s1 on path that might be infeasible incom.mysql.jdbc.ConnectionImpl.nullSafeCompare(String, String)  Dereferenced at ConnectionImpl.java:[line 341]
06   ......
07 M C NP: Method call in com.mysql.jdbc.DatabaseMetaData.getInstance(ConnectionImpl, String) passes null for unconditionally dereferenced parameter of new DatabaseMetaData(ConnectionImpl, String)  Method invoked at DatabaseMetaData.java:[line 632]
08   ......
09 H S SQL: Method com.mysql.jdbc.DatabaseMetaData.getColumnPrivileges(String, String, String, String) passes a nonconstant String to an execute method on an SQL statement  At DatabaseMetaData.java:[line 2156]
10 H S SQL: Method com.mysql.jdbc.DatabaseMetaData.getTablePrivileges(String, String, String) passes a nonconstant String to an execute method on an SQL statement  At DatabaseMetaData.java:[line 4638]
11 M S SQL: Method com.mysql.jdbc.ConnectionImpl.setSessionVariables() passes a nonconstant String to an execute method on an SQL statement  At ConnectionImpl.java:[line 5074]
12   ......
13 M M IS: Inconsistent synchronization of com.mysql.jdbc.CallableStatement.outputParameterResults; locked 50% of time  Unsynchronized access at CallableStatement.java:[line 1948]
14 M M IS: Inconsistent synchronization of com.mysql.jdbc.StatementImpl.wasCancelledByTimeout; locked 83% of time  Unsynchronized access at PreparedStatement.java:[line 1756]
15   ......
16 Warnings generated: 154
17   ......

Learning to read the output of FindBugs takes a little time. What I do is just work through a certain error or warning when I’m bored or frustrated with the code that I should be writing. It’s my procrastination work. :-)

I have only picked out the things that as a developer strike me as problematic: “passes null for unconditionally dereferenced parameter”. Eek. NullPointerException anyone? Of course, this could well be test code, or even unused code that is still under development. How about this: “passes a nonconstant String to an execute method on an SQL statement”. Hmm. If left unchecked, this could be a vector for an SQL injection vulnerability.

I said earlier that FindBugs does not require you to have access to the source code of an application to find bugs in it. Just for laughs, let’s have a look at one of the cornerstones of our Java EE application servers: The Oracle JDBC driver.

01 $ findbugs -textui -effort:max ojdbc6.jar
02   ......
03 M B Dm: oracle.sql.ConverterArchive.openArchiveforRead() invokes System.exit(...), which shuts down the entire virtual machine  At ConverterArchive.java:[line 375]
04 M B Dm: oracle.sql.ConverterArchive.closeArchiveforRead() invokes System.exit(...), which shuts down the entire virtual machine  At ConverterArchive.java:[line 390]
05   ......
06 M B ES: Comparison of String objects using == or != inoracle.jdbc.connector.OracleConnectionRequestInfo.equals(Object)   At OracleConnectionRequestInfo.java:[line 104]
07   ......
08 H C IL: There is an apparent infinite recursive loop inoracle.jdbc.rowset.OracleCachedRowSet.updateBlob(int, InputStream, long)  At OracleCachedRowSet.java:[line 6365]
09 H C IL: There is an apparent infinite recursive loop inoracle.jdbc.rowset.OracleCachedRowSet.updateClob(int, Reader, long)  At OracleCachedRowSet.java:[line 6445]
10 H C IL: There is an apparent infinite recursive loop inoracle.jdbc.rowset.OracleCachedRowSet.updateNClob(int, Reader, long)  At OracleCachedRowSet.java:[line 6535]
11   ......
12 Warnings generated: 1028
13   ......

That driver produces no less than 1028 warnings. Wow. I’m not suggesting that the Oracle JDBC driver is actually a bad piece of code. I just find that it smells a little:-) Oracle’s developers might want to have a crack at resolving the findbugs-reported warnings. There are many little performance and stability suggestions in there.

And yes: findbugs checks for uses of System.gc().

PS. Please be aware that I used FindBugs 1.3.7. This version of FindBugs has a bug that causes it to generate false positives for cleaning up database resources.

分享到:
评论

相关推荐

    Findbugs使用简介

    《Findbugs使用简介》 Findbugs是一款开源的静态代码分析工具,主要针对Java代码进行分析,旨在检测出可能存在的错误和潜在的问题。它通过分析字节码而非源代码,能够发现那些编译器无法捕捉的运行时错误和设计缺陷...

    findbugs 1.3.9 findbugs 1.3.9

    《深入理解FindBugs 1.3.9:静态代码分析的强大工具》 FindBugs是一款广受欢迎的静态代码分析工具,它的版本1.3.9在软件开发领域具有重要的地位。这款开源工具的主要功能是检测Java代码中的潜在错误,帮助开发者在...

    findbugs压缩包+findbugs.jar+findbugs-ant.jar

    标题中的"findbugs压缩包+findbugs.jar+findbugs-ant.jar"指的是该压缩文件内包含FindBugs的主要库文件`findbugs.jar`,这是执行FindBugs分析的核心组件,它包含了各种检测规则和算法。另外,`findbugs-ant.jar`则是...

    FindBugs JSR305_3.0.2

    FindBugs, Find Bugs in Java Programs. FindBugs requires JRE (or JDK) 1.7.0 or later to run. However, it can analyze programs compiled for any version of Java, from 1.0 to 1.8.

    findbugs.jar+findbugs-ant.jar

    FindBugs是一款非常知名的静态代码分析工具,它主要用于在Java代码中发现潜在的错误和缺陷。Eclipse是一款广泛使用的集成开发环境(IDE),对于Java开发者来说尤其重要。将FindBugs集成到Eclipse中,可以帮助开发者...

    FindBugs安装及配置使用说明

    FindBugs是一款强大的Java字节码静态分析工具,它能够帮助开发者在代码执行前发现潜在的缺陷和质量问题。通过检查类文件和JAR包,FindBugs与一系列预定义的缺陷模式进行匹配,从而找出可能存在的问题。这种静态分析...

    findbugs与ant结合

    `path/to/findbugs-ant-task.jar` 应替换为实际的 FindBugs Ant 任务库的路径。 2. 定义 FindBugs 任务: 创建一个 `<findbugs>` 标签,指定要分析的类文件、输出报告的路径以及 FindBugs 的配置参数。例如: `...

    findbugs工具测试java实验报告

    【findbugs工具测试Java实验报告】 在软件开发过程中,代码质量是至关重要的,因为它直接影响到程序的稳定性、可维护性和性能。为了确保代码的质量,开发者通常会采用各种静态代码分析工具,其中FindBugs就是一款...

    网络上搜集的Findbugs在Ant下的配置方法

    **Findbugs在Ant下的配置方法** FindBugs是一款开源的静态代码分析工具,主要用于检测Java代码中的潜在错误、坏味道和安全漏洞。它通过分析字节码而非源代码,能够发现许多编译器无法捕获的问题。在Ant构建环境中...

    FindBugs_Idea

    【标题】FindBugs_Idea:在IntelliJ IDEA中集成与使用FindBugs插件 【描述】本文将详细介绍如何在IntelliJ IDEA这款强大的Java集成开发环境中安装并使用FindBugs插件,该插件是FindBugs-IDEA-1.0.1的最新版本,旨在...

    findbugs3.0.2插件 myeclipse

    FindBugs 是由马里兰大学提供的一款开源 Java静态代码分析工具。FindBugs通过检查类文件或 JAR文件,将字节码与一组缺陷模式进行对比从而发现代码缺陷,完成静态代码分析。FindBugs既提供可视化 UI 界面,同时...

    ecplise插件findbugs2.0

    - **错误等级**:FindBugs 报告通常分为三个等级,分别是 Bug(高优先级)、Possibly Bug(中优先级)和 Less Serious Bug(低优先级)。 - **图标表示**:在代码编辑器中,FindBugs 会以不同颜色的图标标记问题,...

    Findbugs缺陷等级对照表

    Findbugs缺陷等级对照表 FindBugs是一款开源的静态代码分析工具,用于检测Java代码中的缺陷和错误。本文档列出了FindBugs报告的标准缺陷模式,包括正确性、安全性、性能、多线程、坏味道代码等多个方面。 正确性:...

    FindBugs规则(中文版111).doc

    FindBugs是一款静态代码分析工具,它用于检测Java代码中的潜在缺陷。中文版的FindBugs规则文档提供了关于代码质量的多个方面的指导,包括安全、实验性等方面的问题。以下是这些规则的详细解释: 1. Dm: Hardcoded ...

    findbugs最新源代码20081008版本的

    《深入理解FindBugs:基于20081008源码的探索》 FindBugs是一款著名的静态代码分析工具,它通过扫描Java字节码来检测潜在的错误和不良编程习惯,被誉为“程序员的无声守护者”。2008年10月08日的版本是FindBugs发展...

    findbugs-annotations-1.3.9-1-API文档-中文版.zip

    赠送jar包:findbugs-annotations-1.3.9-1.jar; 赠送原API文档:findbugs-annotations-1.3.9-1-javadoc.jar; 赠送源代码:findbugs-annotations-1.3.9-1-sources.jar; 赠送Maven依赖信息文件:findbugs-...

    SonarQube的FindBugs插件sonar-findbugs-plugin.jar,包含FindBugs Security Audit等规则

    SonarQube的FindBugs插件sonar-findbugs-plugin.jar(版本:4.0.1-SNAPSHOT),包含FindBugs Security Audit等规则,可以离线集成到sonarqube。

    findbugs插件使用文档

    `FindBugs`是一款强大的静态代码分析工具,主要用于检测Java代码中的潜在错误和不良实践。它通过分析字节码来找出可能存在的问题,而无需实际运行程序。在Maven项目中,我们可以使用`findbugs-maven-plugin`这个...

    FindBugs安装与使用说明

    java -jar path/to/findbugs.jar -textui -v - Effort: max ``` 这里的`<your_class_files_or_jar>`替换为你的Java类文件或JAR包路径。 2. IDE集成 FindBugs可以与Eclipse、IntelliJ IDEA等主流IDE集成,提供更...

    findbugs插件

    FindBugs是一款非常知名的静态代码分析工具,主要应用于Java程序,用于检测可能存在的错误和缺陷。这个"findbugs2.02"版本是该插件的一个特定迭代,它提供了对Java代码的深度分析,帮助开发者在编码阶段就能发现潜在...

Global site tag (gtag.js) - Google Analytics