Objectives of this webinar
How we detected error patterns
Patterns themselves and how to avoid them:
3.1 Copy-paste and last line effect
3.2 if (A) {...} else if (A)
3.3 Errors in checks
3.4 Array index out of bounds
3.5 Operator precedence
3.6 Typos that are hard to spot
How to use static analysis properly
Conclusion
Q&A
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Typical errors in code on the example of C++, C#, and Java
1. Typical errors in code on the example of
C++, C#, and Java
Information Technology Video Developer Network
Информационный
видеосервис
для
разработчиков
программного
обеспечения
http://itvdn.com
2. Георгий Грибков
About the speaker
Information Technology Video Developer Network http://itvdn.com
ITVDN
C++ developer of the PVS-Studio static code analyzer
• Develops the analyzer core, new diagnostics, supports users.
• Introduced PVS-Studio in the godbolt.org online compiler.
• Wrote articles for the Habr website and gave talks at IT conferences,
related to searching for bugs in code.
Typical errors in code on the example of C++, C#, and Java
3. Agenda
Information Technology Video Developer Network http://itvdn.com
ITVDN
1. Objectives of this webinar
2. How we detected error patterns
3. Patterns themselves and how to avoid them:
3.1 Copy-paste and last line effect
3.2 if (A) {...} else if (A)
3.3 Errors in checks
3.4 Array index out of bounds
3.5 Operator precedence
3.6 Typos that are hard to spot
4. How to use static analysis properly
5. Conclusion
6. Q&A
Заключение
Typical errors in code on the example of C++, C#, and Java
4. Information Technology Video Developer Network http://itvdn.com
ITVDN
More than 50 video courses for C# developers at ITVDN
C# Basics
Author: Alexander Shevchuk
Duration: 16 h 3 mins
9 lessons
C# Basic (OOP)
Author: Alexander Shevchuk
Duration: 31 h 26 mins
18 lessons
C# for professionals
Author: Oleg Kulygin
Duration: 19 h 38 mins
17 lessons
C# Generics
Author: Nikolay Melnichuk
Duration: 4 h 49 mins
7 lessons
Unit testing in C#
Author: Dmitry Okhrimenko
Duration: 3 h 48 mins
3 lessons
.NET Apps
Refactoring
Author: David Boyarov
Duration: 6 h 41 mins, 5 lessons
5. Information Technology Video Developer Network http://itvdn.com
ITVDN
More than 26 video courses for Java developers at ITVDN
Java Starter
Author: Evgeny Tikhonov
Duration: 9 h 46 mins
9 lessons
Java Essential
Author: Evgeny Tikhonov
Duration: 11 h 10 mins
10 lessons
Java Professional
Author : Evgeny Tikhonov
Duration: 20 h 18 mins
15 lessons
SOLID principles in Java
Author: Andrey Fok
Duration: 2 h 45 mins
5 lessons
Unit testing in Java with JUnit
Author: Mikhail Skafenko
Duration: 2 h 33 mins
7 lessons
Java EE Basics
Author: Andrey Bondarenko
Duration: 18 h 50 mins
12 lessons
6. Information Technology Video Developer Network http://itvdn.com
ITVDN
Video courses for C++ developers at ITVDN
C++ Starter
Author: Vladimir Vinogradov
Duration: 8 h 13 mins
13 lessons
QT Framework
Author: Ruslan Larionenko
Duration: 6 h 27 mins
10 lessons
C++ Essential
Author: Kirill Chernega
Duration: 4 h 38 mins
8 lessons
C++Advanced
Author: Kirill Chernega
Duration: 8 h 17 mins
11 lessons
Complete practical tasks in C++
Author: Naumenko Alexander
Duration: 4 h 39 mins, 7 lessons
STL - Standard Template Library
Author: Pavlenko Alexander
Duration: 7 h 5 mins, 12 lessons
7. Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
8. Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
9. Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
10. Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
11. Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
12. Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
13. Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
14. Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
15. Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
16. Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
17. Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
18. Information Technology Video Developer Network http://itvdn.com
ITVDN
*
Typical errors in code on the example of C++, C#, and Java
19. Information Technology Video Developer Network http://itvdn.com
ITVDN
BUGS ARE EVERYWHERE!
Typical errors in code on the example of C++, C#, and Java
20. How to avoid errors
Information Technology Video Developer Network http://itvdn.com
ITVDN
• Warn developers of typical problems
• Use tools to automatically search for errors
Typical errors in code on the example of C++, C#, and Java
21. Objectives of this webinar
Information Technology Video Developer Network http://itvdn.com
ITVDN
• Demonstrate typical error patterns in code
• Show how to use static analysis properly
Typical errors in code on the example of C++, C#, and Java
22. 2. How we detected error patterns
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
23. What is static analysis
Information Technology Video Developer Network http://itvdn.com
ITVDN
Static analysis is automated code review.
Typical errors in code on the example of C++, C#, and Java
24. What is static analysis
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
25. Examples of static analyzers
Information Technology Video Developer Network http://itvdn.com
ITVDN
• PVS-Studio
• Cppcheck
• Infer
• IntelliJ IDEA
• Clang Static Analyzer
• FindBugs
• ...
Long list of static analyzers:
Typical errors in code on the example of C++, C#, and Java
26. How we detected error patterns
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
Found errors
380 13747
Checked
projects
Detected
errors
27. Check out the base of errors we found:
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
28. 3. Patterns themselves and how to avoid them
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
29. Pattern № 1:
Copy-paste and last line effect
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
31. Elasticsearch (Java)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning:
V6039 There are two 'if' statements with identical
conditional expressions. The first 'if' statement
contains method return. This means that the
second 'if' statement is senseless.
Typical errors in code on the example of C++, C#, and Java
32. Information Technology Video Developer Network http://itvdn.com
ITVDN
Clang (C++)
Typical errors in code on the example of C++, C#, and Java
33. Information Technology Video Developer Network http://itvdn.com
ITVDN
Clang (C++)
PVS-Studio warning: V501 There are identical sub-expressions
SM.getExpansionColumnNumber(ContainerREnd)' to the left and to the right of the '>=' operator.
Typical errors in code on the example of C++, C#, and Java
34. Information Technology Video Developer Network http://itvdn.com
ITVDN
Clang (C++)
PVS-Studio warning: V501 There are identical sub-expressions
SM.getExpansionColumnNumber(ContainerREnd)' to the left and to the right of the '>=' operator.
Typical errors in code on the example of C++, C#, and Java
35. Xenko Game Engine (C#)
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
36. Xenko Game Engine (C#)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning: V3001 There are
identical sub-expressions 'box.Maximum.X
- box.Minimum.X > sphere.Radius' to the
left and to the right of the '&&' operator.
Typical errors in code on the example of C++, C#, and Java
37. Pattern № 1: Copy-paste and last line effect
Information Technology Video Developer Network http://itvdn.com
ITVDN
• We selected 84 examples of erroneous code
written with copy-paste
• 43 of them had an error in the last line!
• It is more than 50%!
Typical errors in code on the example of C++, C#, and Java
38. Information Technology Video Developer Network http://itvdn.com
ITVDN
• Stop copy-pasting
• Copy-pasting in programming is pure evil!
• If you dare to – be extremely attentive
How to avoid
Typical errors in code on the example of C++, C#, and Java
39. Pattern № 2.
if (A) {...} else if (A)
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
41. Elasticsearch (Java)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning:
V6003 The use of 'if (A) {....} else if (A)
{....}' pattern was detected. There is a
probability of logical error presence.
Typical errors in code on the example of C++, C#, and Java
43. Chromium (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning:
V517 The use of 'if (A) {...} else if (A)
{...}' pattern was detected. There is a
probability of logical error presence.
Check lines: 61, 63.
Typical errors in code on the example of C++, C#, and Java
44. CryEngine V (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
45. CryEngine V (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning:
V517 The use of 'if (A) {...} else if (A)
{...}' pattern was detected. There is a
probability of logical error presence.
Check lines: 266, 268.
Typical errors in code on the example of C++, C#, and Java
46. CryEngine V (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning:
V517 The use of 'if (A) {...} else if (A)
{...}' pattern was detected. There is a
probability of logical error presence.
Check lines: 266, 268.
Typical errors in code on the example of C++, C#, and Java
52. MonoDevelop (C#)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning:
V3021 There are two 'if' statements
with identical conditional
expressions. The first 'if' statement
contains method return. This means
that the second 'if' statement is
senseless.
Typical errors in code on the example of C++, C#, and Java
53. Information Technology Video Developer Network http://itvdn.com
ITVDN
• Try to do best not to copy-paste
• If you’re still going to copy-paste, copy non-compiled constructions.
• Example:
if (value == _)
return _;
How to avoid?
Typical errors in code on the example of C++, C#, and Java
54. Pattern № 3.
Errors in checks
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
55. Unity (C#)
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
56. Unity (C#)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning:
V3063 A part of conditional
expression is always true if it is
evaluated: pageSize <= 1000.
Typical errors in code on the example of C++, C#, and Java
57. Bullet - the engine of Red Dead Redemption (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
58. Bullet - the engine of Red Dead Redemption (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning:
V709 Suspicious comparison found:
'f0 == f1 == m_fractureBodies.size()’.
Remember that
'a == b == c’
is not equal to
'a == b && b == c'.
Typical errors in code on the example of C++, C#, and Java
59. Apache Hive (Java)
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
60. Apache Hive (Java)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning: V6030 The method located to the right of the '|' operator will be called regardless of
the value of the left operand. Perhaps, it is better to use '||'.
Typical errors in code on the example of C++, C#, and Java
63. Chromium (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warnings:
V547 Expression
'time.month <=
kDaysInMonth[time.month] + 1' is
always true.
V547 Expression
'time.month <=
kDaysInMonth[time.month]’
is always true.
Typical errors in code on the example of C++, C#, and Java
64. Information Technology Video Developer Network http://itvdn.com
ITVDN
• «Pay attention!» won’t work every time
• Try backup tools
How to avoid
Typical errors in code on the example of C++, C#, and Java
65. Pattern № 4.
Array index out of bounds
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
67. Stickies (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning:
V557 Array overrun is possible. The
'64' index is pointing beyond array
bound. stickies stickies.cpp
Typical errors in code on the example of C++, C#, and Java
69. Elasticsearch (Java)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning: V6025 Possibly index '(int) x' is out of bounds.
Typical errors in code on the example of C++, C#, and Java
70. Elasticsearch (Java)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning: V6025 Possibly index '(int) x' is out of bounds.
Typical errors in code on the example of C++, C#, and Java
71. IPP Samples (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
72. IPP Samples (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
73. IPP Samples (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
74. IPP Samples (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
75. IPP Samples (C++)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning:
V557 Array overrun is possible. The '30'
index is pointing beyond array bound.
Typical errors in code on the example of C++, C#, and Java
77. FastReport (C#)
Information Technology Video Developer Network http://itvdn.com
ITVDN
PVS-Studio warning:
V3106 Possible negative index value. The value
of 'idx' index could reach -1.
Typical errors in code on the example of C++, C#, and Java
78. Information Technology Video Developer Network http://itvdn.com
ITVDN
• Be careful when you add '0’ to the end of the string
• Do you get an external index? Make sure you check it!
• Don’t mix up '>' ('<‘) and '>=' ('<=‘) when comparing the index with the array size
How to avoid
Typical errors in code on the example of C++, C#, and Java
79. 4. How to use static analysis properly
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
80. When you should perform static analysis
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
81. When you should perform static analysis
Information Technology Video Developer Network http://itvdn.com
ITVDN
Static analysis
Typical errors in code on the example of C++, C#, and Java
82. How to make the most of static analysis
Information Technology Video Developer Network http://itvdn.com
ITVDN
• Apply static analysis at early stages
• Analyze regularly
Typical errors in code on the example of C++, C#, and Java
99. More about analysis of commits and pull-requests
Information Technology Video Developer Network http://itvdn.com
ITVDN
Typical errors in code on the example of C++, C#, and Java
101. Typical error patterns in code
Information Technology Video Developer Network http://itvdn.com
ITVDN
• Copy-paste and last line effect
• if (A) {...} else if (A)
• Errors in checks
• Array index out of bounds
• … (the list gradually expands)
Typical errors in code on the example of C++, C#, and Java
102. How to avoid typical errors
Information Technology Video Developer Network http://itvdn.com
ITVDN
• Stop copy-pasting!
• Seriously, stop copy-pasting!
• Pay attention to checks, even small and short ones.
• Carefully check array indexes.
• Regularly use static analysis.
Typical errors in code on the example of C++, C#, and Java
103. Free PVS-Studio license for students
Information Technology Video Developer Network http://itvdn.com
ITVDN
https://bit.ly/pvs-student
Typical errors in code on the example of C++, C#, and Java
104. Information Technology Video Developer Network http://itvdn.com
ITVDN
Q&A
Typical errors in code on the example of C++, C#, and Java
105. Information Technology Video Developer Network http://itvdn.com
ITVDN
Watch our video lessons on C++
At ITVDN you’ll find a collection of video courses and webinars for C++ developers.
Go to ITVDN.com and watch our video lessons right now!
106. IT VIDEO DEVELOPERS NETWORK
Information Technology Video Developer Network http://itvdn.com
ITVDN