SlideShare a Scribd company logo
Static analysis: Around Java in 60 minutes
Maxim Stefanov
PVS-Studio, C++/Java developer, Tula
1
About the speaker
• Maxim Stefanov (stefanov@viva64.com)
• C++/Java developer in the PVS-Studio company
• Activities:
• Taking part in developing the C++ analyser core
• Taking part in developing the Java analyzer
2
We’re going to talk about...
• Theory
• Code quality (bugs, vulnerabilities)
• Methodologies of code protection against defects
• Code Review
• Static analysis and everything related to it
• Tools
• Existing tools of static analysis
• SonarQube
• PVS-Studio for Java what is it?
• Several detected examples of code with defects
• More about static analysis
• Conclusions
3
Why we need to concern about code quality
• Don’t let technical debt accrue, if a project is green
• Don’t lose users, if a project already has a history
4
Cost of fixing a defect
From the book by C. McConnell "Code Complete"
5
Methods to provide the code of high quality
6
Code Review
7
Pros Cons
Detection of defects at the earliest development stage Tiring
Intensified teamwork Time-consuming
Increased degree of code grasping Expensive
Learning effect
Fresh outside perspective
(no matter how cool you are as a programmer, you’ll
definitely forget something)
Detection of high level errors
8
Code Review
Static code analysis
Pros Cons
Detects defects before code reviewing You cannot detect high level
errors
The analyser doesn’t get tired and is ready to work
anytime
False positives
You can find some errors not knowing about such patterns
You can detect errors that are difficult to notice when
reviewing code
9
Technologies used in static analysis
•Pattern-based analysis
•Type inference
•Data-flow analysis
•Symbolic execution
•Method annotations
10
Pattern-based analysis
@Override
public boolean equals(Object obj) {
....
return index.equals(other.index)
&& type.equals(other.type)
&& version == other.version
&& found == other.found
&& tookInMillis == tookInMillis
&& Objects.equals(terms, other.terms);
}
11
Type inference
interface Human { ... }
class Parent implements Human{ ... }
class Child extends Parent { ... }
...
class Animal { ... }
...
boolean someMethod(List<Child> list, Animal animal)
{
if (list.remove(animal))
return false;
...
}
12
Method annotations
Class("java.lang.Math")
- Function("max", Type::Int32, Type::Int32)
.Pure()
.Set(FunctionClassification::NoDiscard)
.Requires(NotEquals(Arg1, Arg2))
.Returns(Arg1, Arg2, [](const Int &v1, const Int &v2)
{
return v1.Max(v2);
}
)
13
Method annotations
int test(int a, int b) {
Math.max(a, b); //1
if (a > 5 && b < 2) {
// a = [6..INT_MAX]
// b = [INT_MIN..1]
if (Math.max(a, b) > 0) //2
{...}
}
return Math.max(a, a); //3
}
14
Data-flow analysis
void func(int x) // x: [-2147483648..2147483647] //1
{
if (x > 3)
{
// x: [4..2147483647] //2
if (x < 10)
{
// x: [4..9] //3
}
}
else
{
// x: [-2147483648..3] //4
}
}
15
Symbolic execution
int someMethod(int A, int B)
{
if (A == B)
return 10 / (A - B);
return 1;
}
16
Existing tools
17
SonarQube: who, what and why
• Platform with open source code for continuous analysis and
estimating the code quality
• Contains a number of analyzers for various languages
• Allows to integrate third-party analyzers
• Clearly demonstrates quality of your project
18
SonarQube: data representation
19
SonarQube: data representation
20
SonarQube: data representation
21
SonarQube: data representation
22
Story of creating PVS-Studio for Java
• Java is a popular language
• Wide implementation area of the language
• We could use mechanisms from the C++ analyzer
(data-flow analysis, method annotations)
23
Analyzer internals
24
Spoon for getting a syntax tree and semantic
model
Spoon transforms the code in the metamodel:
class TestClass
{
void test(int a, int b)
{
int x = (a + b) * 4;
System.out.println(x);
}
}
25
Analyzer internals
Data-flow analysis, method annotations - usage of mechanisms from
the C++ analyzer using SWIG
26
Analyzer internals
Diagnostic rule is a visitor with overloaded methods.
Inside the methods the items that are of interest for us are traversed
along the tree.
27
Analyzer internals
Several examples of errors, found using
PVS-Studio
28
Integer division
private static boolean checkSentenceCapitalization(@NotNull String value) {
List<String> words = StringUtil.split(value, " ");
....
int capitalized = 1;
....
return capitalized / words.size() < 0.2; // allow reasonable amount of
// capitalized words
}
V6011 [CWE-682] The '0.2' literal of the 'double' type is compared to a value of the 'int' type.
TitleCapitalizationInspection.java 169
IntelliJ IDEA
29
Always false
PVS-Studio: V6007 [CWE-570] Expression '"0".equals(text)' is always false. ConvertIntegerToDecimalPredicate.java 46
IntelliJ IDEA
public boolean satisfiedBy(@NotNull PsiElement element) {
....
@NonNls final String text = expression.getText().replaceAll("_", "");
if (text == null || text.length() < 2) {
return false;
}
if ("0".equals(text) || "0L".equals(text) || "0l".equals(text)) {
return false;
}
return text.charAt(0) == '0';
}
30
Unexpected number of iterations
public static String getXMLType(@WillNotClose InputStream in) throws
IOException
{
....
String s;
int count = 0;
while (count < 4) {
s = r.readLine();
if (s == null) {
break;
}
Matcher m = tag.matcher(s);
if (m.find()) {
return m.group(1);
}
}
....
}
31
SpotBugs
V6007 [CWE-571] Expression 'count < 4' is always true. Util.java 394
We can’t go on without Copy-Paste
public class RuleDto {
....
private final RuleDefinitionDto definition;
private final RuleMetadataDto metadata;
....
private void setUpdatedAtFromDefinition(@Nullable Long updatedAt) {
if (updatedAt != null && updatedAt > definition.getUpdatedAt()) {
setUpdatedAt(updatedAt);
}
}
private void setUpdatedAtFromMetadata(@Nullable Long updatedAt) {
if (updatedAt != null && updatedAt > definition.getUpdatedAt()) {
setUpdatedAt(updatedAt);
}
}
....
}
32
SonarQube
V6032 It is odd that the body of method 'setUpdatedAtFromDefinition' is fully equivalent to the body of another method
'setUpdatedAtFromMetadata'. Check lines: 396, 405. RuleDto.java 396
Duplicates
V6033 [CWE-462] An item with the same key 'JavaPunctuator.PLUSEQU' has already been added. Check lines: 104, 100.
KindMaps.java 104
SonarJava
private final Map<JavaPunctuator, Tree.Kind> assignmentOperators =
Maps.newEnumMap(JavaPunctuator.class);
public KindMaps() {
....
assignmentOperators.put(JavaPunctuator.PLUSEQU, Tree.Kind.PLUS_ASSIGNMENT);
....
assignmentOperators.put(JavaPunctuator.PLUSEQU, Tree.Kind.PLUS_ASSIGNMENT);
....
}
33
How to integrate static analysis in the process
of software development
• Each developer has a static analysis tool on his machine
• Analysis of the entire code base during the night builds.
When suspicious code is found - all guilty ones get
mails.
34
How to start using static analysis tools on large
projects and not to lose heart
1. Check the project
2. Specify that all issued warnings are not interesting for us yet.
Place the warnings in a special suppression file
3. Upload the file with markup in the version control system
4. Run the analyser and get warnings only for the newly written or
modified code
5. PROFIT!
35
Conclusions
• Static analysis – additional methodology, not a «silver bullet»
• Static analysis has to be used regularly
• You can immediately start using the analysis and postpone fixing of
old errors
• Competition is a key to progress
36
Maxim Stefanov
stefanov@viva64.com
7 953 968 49 43
37

More Related Content

PPTX
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
PPTX
Java 104
PPTX
Java 101
PDF
Important java programs(collection+file)
PPTX
TDD Training
DOC
Advanced Java - Praticals
DOCX
Java PRACTICAL file
PPTX
saihw1_weka_tutorial.pptx - Machine Discovery and Social Network ...
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
Java 104
Java 101
Important java programs(collection+file)
TDD Training
Advanced Java - Praticals
Java PRACTICAL file
saihw1_weka_tutorial.pptx - Machine Discovery and Social Network ...

What's hot (20)

PDF
OOPs & Inheritance Notes
PPTX
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
DOC
Ad java prac sol set
PPT
Core java concepts
PDF
Java Collections API
PDF
4java Basic Syntax
PPT
Java Tutorials
PDF
RxJava from the trenches
DOCX
Advance Java Programs skeleton
PPTX
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
PPT
java training faridabad
PDF
ZIP
Elementary Sort
PDF
Java OOP Programming language (Part 8) - Java Database JDBC
PDF
Java OOP Programming language (Part 4) - Collection
PDF
Java 7 New Features
PDF
Procedure Typing for Scala
PPTX
Java 101 Intro to Java Programming - Exercises
PPTX
Chap2 class,objects contd
OOPs & Inheritance Notes
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
Ad java prac sol set
Core java concepts
Java Collections API
4java Basic Syntax
Java Tutorials
RxJava from the trenches
Advance Java Programs skeleton
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
java training faridabad
Elementary Sort
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 4) - Collection
Java 7 New Features
Procedure Typing for Scala
Java 101 Intro to Java Programming - Exercises
Chap2 class,objects contd
Ad

Similar to Static analysis: Around Java in 60 minutes (20)

PPTX
Static code analysis: what? how? why?
PPTX
The operation principles of PVS-Studio static code analyzer
PPTX
Class and Object.pptx from nit patna ece department
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
PPT
Topic2JavaBasics.ppt
PPT
PPT
hallleuah_java.ppt
PPT
JAVA BASICS
PDF
Spring Day | Spring and Scala | Eberhard Wolff
PPT
Core java by a introduction sandesh sharma
PPTX
Machine Learning - Simple Linear Regression
PPT
PPT
Introduction to Java(basic understanding).ppt
PPTX
Programming in java basics
PDF
Mining Source Code Improvement Patterns from Similar Code Review Works
PPTX
Mining Source Code Improvement Patterns from Similar Code Review Works
PPTX
JavaScript code academy - introduction
PPT
Jdbc oracle
PDF
Headache from using mathematical software
PDF
How to fake_properly
Static code analysis: what? how? why?
The operation principles of PVS-Studio static code analyzer
Class and Object.pptx from nit patna ece department
Class and Object JAVA PROGRAMMING LANG .pdf
Topic2JavaBasics.ppt
hallleuah_java.ppt
JAVA BASICS
Spring Day | Spring and Scala | Eberhard Wolff
Core java by a introduction sandesh sharma
Machine Learning - Simple Linear Regression
Introduction to Java(basic understanding).ppt
Programming in java basics
Mining Source Code Improvement Patterns from Similar Code Review Works
Mining Source Code Improvement Patterns from Similar Code Review Works
JavaScript code academy - introduction
Jdbc oracle
Headache from using mathematical software
How to fake_properly
Ad

More from Andrey Karpov (20)

PDF
60 антипаттернов для С++ программиста
PDF
60 terrible tips for a C++ developer
PPTX
Ошибки, которые сложно заметить на code review, но которые находятся статичес...
PDF
PVS-Studio in 2021 - Error Examples
PDF
PVS-Studio in 2021 - Feature Overview
PDF
PVS-Studio в 2021 - Примеры ошибок
PDF
PVS-Studio в 2021
PPTX
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
PPTX
Best Bugs from Games: Fellow Programmers' Mistakes
PPTX
Does static analysis need machine learning?
PPTX
Typical errors in code on the example of C++, C#, and Java
PPTX
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
PPTX
Game Engine Code Quality: Is Everything Really That Bad?
PPTX
C++ Code as Seen by a Hypercritical Reviewer
PPTX
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
PPTX
Static Code Analysis for Projects, Built on Unreal Engine
PPTX
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
PPTX
The Great and Mighty C++
PDF
Zero, one, two, Freddy's coming for you
PDF
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
60 антипаттернов для С++ программиста
60 terrible tips for a C++ developer
Ошибки, которые сложно заметить на code review, но которые находятся статичес...
PVS-Studio in 2021 - Error Examples
PVS-Studio in 2021 - Feature Overview
PVS-Studio в 2021 - Примеры ошибок
PVS-Studio в 2021
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Best Bugs from Games: Fellow Programmers' Mistakes
Does static analysis need machine learning?
Typical errors in code on the example of C++, C#, and Java
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
Game Engine Code Quality: Is Everything Really That Bad?
C++ Code as Seen by a Hypercritical Reviewer
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
Static Code Analysis for Projects, Built on Unreal Engine
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
The Great and Mighty C++
Zero, one, two, Freddy's coming for you
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps

Recently uploaded (20)

PDF
System and Network Administraation Chapter 3
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
history of c programming in notes for students .pptx
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
L1 - Introduction to python Backend.pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Digital Strategies for Manufacturing Companies
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPT
Introduction Database Management System for Course Database
System and Network Administraation Chapter 3
Upgrade and Innovation Strategies for SAP ERP Customers
history of c programming in notes for students .pptx
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
L1 - Introduction to python Backend.pptx
PTS Company Brochure 2025 (1).pdf.......
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
How to Choose the Right IT Partner for Your Business in Malaysia
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Digital Strategies for Manufacturing Companies
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
ISO 45001 Occupational Health and Safety Management System
Wondershare Filmora 15 Crack With Activation Key [2025
Odoo Companies in India – Driving Business Transformation.pdf
Design an Analysis of Algorithms I-SECS-1021-03
Online Work Permit System for Fast Permit Processing
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Introduction Database Management System for Course Database

Static analysis: Around Java in 60 minutes

  • 1. Static analysis: Around Java in 60 minutes Maxim Stefanov PVS-Studio, C++/Java developer, Tula 1
  • 2. About the speaker • Maxim Stefanov (stefanov@viva64.com) • C++/Java developer in the PVS-Studio company • Activities: • Taking part in developing the C++ analyser core • Taking part in developing the Java analyzer 2
  • 3. We’re going to talk about... • Theory • Code quality (bugs, vulnerabilities) • Methodologies of code protection against defects • Code Review • Static analysis and everything related to it • Tools • Existing tools of static analysis • SonarQube • PVS-Studio for Java what is it? • Several detected examples of code with defects • More about static analysis • Conclusions 3
  • 4. Why we need to concern about code quality • Don’t let technical debt accrue, if a project is green • Don’t lose users, if a project already has a history 4
  • 5. Cost of fixing a defect From the book by C. McConnell "Code Complete" 5
  • 6. Methods to provide the code of high quality 6
  • 8. Pros Cons Detection of defects at the earliest development stage Tiring Intensified teamwork Time-consuming Increased degree of code grasping Expensive Learning effect Fresh outside perspective (no matter how cool you are as a programmer, you’ll definitely forget something) Detection of high level errors 8 Code Review
  • 9. Static code analysis Pros Cons Detects defects before code reviewing You cannot detect high level errors The analyser doesn’t get tired and is ready to work anytime False positives You can find some errors not knowing about such patterns You can detect errors that are difficult to notice when reviewing code 9
  • 10. Technologies used in static analysis •Pattern-based analysis •Type inference •Data-flow analysis •Symbolic execution •Method annotations 10
  • 11. Pattern-based analysis @Override public boolean equals(Object obj) { .... return index.equals(other.index) && type.equals(other.type) && version == other.version && found == other.found && tookInMillis == tookInMillis && Objects.equals(terms, other.terms); } 11
  • 12. Type inference interface Human { ... } class Parent implements Human{ ... } class Child extends Parent { ... } ... class Animal { ... } ... boolean someMethod(List<Child> list, Animal animal) { if (list.remove(animal)) return false; ... } 12
  • 13. Method annotations Class("java.lang.Math") - Function("max", Type::Int32, Type::Int32) .Pure() .Set(FunctionClassification::NoDiscard) .Requires(NotEquals(Arg1, Arg2)) .Returns(Arg1, Arg2, [](const Int &v1, const Int &v2) { return v1.Max(v2); } ) 13
  • 14. Method annotations int test(int a, int b) { Math.max(a, b); //1 if (a > 5 && b < 2) { // a = [6..INT_MAX] // b = [INT_MIN..1] if (Math.max(a, b) > 0) //2 {...} } return Math.max(a, a); //3 } 14
  • 15. Data-flow analysis void func(int x) // x: [-2147483648..2147483647] //1 { if (x > 3) { // x: [4..2147483647] //2 if (x < 10) { // x: [4..9] //3 } } else { // x: [-2147483648..3] //4 } } 15
  • 16. Symbolic execution int someMethod(int A, int B) { if (A == B) return 10 / (A - B); return 1; } 16
  • 18. SonarQube: who, what and why • Platform with open source code for continuous analysis and estimating the code quality • Contains a number of analyzers for various languages • Allows to integrate third-party analyzers • Clearly demonstrates quality of your project 18
  • 23. Story of creating PVS-Studio for Java • Java is a popular language • Wide implementation area of the language • We could use mechanisms from the C++ analyzer (data-flow analysis, method annotations) 23
  • 25. Spoon for getting a syntax tree and semantic model Spoon transforms the code in the metamodel: class TestClass { void test(int a, int b) { int x = (a + b) * 4; System.out.println(x); } } 25 Analyzer internals
  • 26. Data-flow analysis, method annotations - usage of mechanisms from the C++ analyzer using SWIG 26 Analyzer internals
  • 27. Diagnostic rule is a visitor with overloaded methods. Inside the methods the items that are of interest for us are traversed along the tree. 27 Analyzer internals
  • 28. Several examples of errors, found using PVS-Studio 28
  • 29. Integer division private static boolean checkSentenceCapitalization(@NotNull String value) { List<String> words = StringUtil.split(value, " "); .... int capitalized = 1; .... return capitalized / words.size() < 0.2; // allow reasonable amount of // capitalized words } V6011 [CWE-682] The '0.2' literal of the 'double' type is compared to a value of the 'int' type. TitleCapitalizationInspection.java 169 IntelliJ IDEA 29
  • 30. Always false PVS-Studio: V6007 [CWE-570] Expression '"0".equals(text)' is always false. ConvertIntegerToDecimalPredicate.java 46 IntelliJ IDEA public boolean satisfiedBy(@NotNull PsiElement element) { .... @NonNls final String text = expression.getText().replaceAll("_", ""); if (text == null || text.length() < 2) { return false; } if ("0".equals(text) || "0L".equals(text) || "0l".equals(text)) { return false; } return text.charAt(0) == '0'; } 30
  • 31. Unexpected number of iterations public static String getXMLType(@WillNotClose InputStream in) throws IOException { .... String s; int count = 0; while (count < 4) { s = r.readLine(); if (s == null) { break; } Matcher m = tag.matcher(s); if (m.find()) { return m.group(1); } } .... } 31 SpotBugs V6007 [CWE-571] Expression 'count < 4' is always true. Util.java 394
  • 32. We can’t go on without Copy-Paste public class RuleDto { .... private final RuleDefinitionDto definition; private final RuleMetadataDto metadata; .... private void setUpdatedAtFromDefinition(@Nullable Long updatedAt) { if (updatedAt != null && updatedAt > definition.getUpdatedAt()) { setUpdatedAt(updatedAt); } } private void setUpdatedAtFromMetadata(@Nullable Long updatedAt) { if (updatedAt != null && updatedAt > definition.getUpdatedAt()) { setUpdatedAt(updatedAt); } } .... } 32 SonarQube V6032 It is odd that the body of method 'setUpdatedAtFromDefinition' is fully equivalent to the body of another method 'setUpdatedAtFromMetadata'. Check lines: 396, 405. RuleDto.java 396
  • 33. Duplicates V6033 [CWE-462] An item with the same key 'JavaPunctuator.PLUSEQU' has already been added. Check lines: 104, 100. KindMaps.java 104 SonarJava private final Map<JavaPunctuator, Tree.Kind> assignmentOperators = Maps.newEnumMap(JavaPunctuator.class); public KindMaps() { .... assignmentOperators.put(JavaPunctuator.PLUSEQU, Tree.Kind.PLUS_ASSIGNMENT); .... assignmentOperators.put(JavaPunctuator.PLUSEQU, Tree.Kind.PLUS_ASSIGNMENT); .... } 33
  • 34. How to integrate static analysis in the process of software development • Each developer has a static analysis tool on his machine • Analysis of the entire code base during the night builds. When suspicious code is found - all guilty ones get mails. 34
  • 35. How to start using static analysis tools on large projects and not to lose heart 1. Check the project 2. Specify that all issued warnings are not interesting for us yet. Place the warnings in a special suppression file 3. Upload the file with markup in the version control system 4. Run the analyser and get warnings only for the newly written or modified code 5. PROFIT! 35
  • 36. Conclusions • Static analysis – additional methodology, not a «silver bullet» • Static analysis has to be used regularly • You can immediately start using the analysis and postpone fixing of old errors • Competition is a key to progress 36