The Use of Static Code Analysis When Teaching or Developing Open-Source Software Presenter: George Gribkov
1. Static analysis: short overview 2. Use of static analysis at colleges and universities 3. Use of static analysis in student and open projects Contents 2
Static Analysis: Short Overview 3
 Write correct code  Unit tests  Regression testing  Code review  …is there some other way?  Yes! For example – tools for automated analysis. How to Improve Code Quality 4
 Static analysis tools: check code when it’s not executed  Dynamic analysis tools: check code when it’s being executed Automated Code Analysis Tools 5  Both approaches compliment each other very well.
Cost to Fix a Bug 6
 Issues false positives  Difficulties with multithreading  Does not eliminate the need for code review Static Analysis Disadvantages 7
 Covers the entire code  Significantly faster than dynamic code analysis  More convenient for large projects Static Analysis Advantages 8
 Can check code style or whether the code complies with a coding standard (MISRA, AUTOSAR C++)  Easy to use  Helps developers learn and teach Static Analysis Advantages 9
Use of Static Analysis at Colleges and Universitites 10
 Helps check homework  Helps check final projects  Saves instructors’ time For Instructors 11
 Provides a chance to learn a new approach  Helps with self-study and problem solving  Facilitates development  Shows and helps study error patterns For Students 12
Pattern Examples (Vangers) 13 void aciPackFile(....) { int sz,sz1; char *p,*p1; .... p = new char[sz]; p1 = new char[sz1]; .... delete p; delete p1; }
Pattern Examples (Vangers) 14 void aciPackFile(....) { int sz,sz1; char *p,*p1; .... p = new char[sz]; p1 = new char[sz1]; .... delete p; // <= delete p1; // <= }
Pattern Examples (Vangers) 15 void aciPackFile(....) { int sz,sz1; char *p,*p1; .... p = new char[sz]; p1 = new char[sz1]; .... delete [] p; delete [] p1; }
Pattern Examples (Apache HTTP Server) 16 static void MD4Transform( apr_uint32_t state[4], const unsigned char block[64]) { apr_uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[APR_MD4_DIGESTSIZE]; .... /* Zeroize sensitive information. */ memset(x, 0, sizeof(x)); }
Pattern Examples (Apache HTTP Server) 17 static void MD4Transform( apr_uint32_t state[4], const unsigned char block[64]) { apr_uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[APR_MD4_DIGESTSIZE]; .... /* Zeroize sensitive information. */ memset(x, 0, sizeof(x)); // <= }
Pattern Examples (Apache HTTP Server) 18 static void MD4Transform( apr_uint32_t state[4], const unsigned char block[64]) { apr_uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[APR_MD4_DIGESTSIZE]; .... /* Zeroize sensitive information. */ memset_s(x, 0, sizeof(x)); } *Or use the following flag: -fno-builtin-memset!
 Provides a chance to learn a new approach  Helps with self-study and problem solving  Facilitates development  Shows and helps study error patterns For Students 19
Use of Static Analysis in Student and Open Projects 20
 Static analysis provides its maximum benefit only when used regularly! Regular Use is the Main Thing 21
Regular Use is the Main Thing 22
Efficient Static Analyzers 23 • PVS-Studio • Clang Static Analyzer • Cppcheck • Infer • IntelliJ IDEA • FindBugs • ... • A detailed list of static analyzers:
1. A classic development scenario (in office) 2. Developing student and open-source projects Introducing Analysis 24
 Locally on developers’ computer (plugins for IDE, compilation monitoring system) A Typical Scenario 25
 Continuous integration systems (command-line utilities, plugins for CI systems, monitoring systems) A Typical Scenario 26
A Typical Scenario 27
What’s the difference? Student and Open-Source Projects 28
A Typical Scenario 29
Student and Open-Source Projects 30
Student and Open-Source Projects 31
Student and Open-Source Projects 32
Student and Open-Source Projects 33
Using an Analyzer on Open-Source Projects 34
Using an Analyzer on Open-Source Projects 35
How to Analyze Community Contribution? 36
What to Do After the First Check? 37
Using an Analyzer on Open-Source Projects 38
Using an Analyzer on Open-Source Projects 39
Pull Request Analysis 40
How to Analyze Community Contribution? 41
 Suppress bases are a mass suppression tool for the analyzer’s warnings. After the First Check 42
 Suppress bases are a mass suppression tool for the analyzer’s warnings. After the First Check 43
 Hide old errors – keep up the normal pace  See only the latest warnings starting from this moment  Get immediate benefits from the analyzer  Do not forget about the old errors! Come back and fix them one-by-one. The Purpose of Suppress Bases 44
 A very convenient approach: the “ratcheting” method  The number of errors in the base is committed to the repository.  Changes are allowed only when they do not increase the total number of errors. How to Work with Suppress Base 45
How to Work with Suppress Base 46
 https://habr.com/en/post/440610/ An Article on the Topic 47
Conclusion 48
 Static analysis helps study programming  It’s important to use static analysis regularly  It’s okay to use static analysis in open-source projects! Recap 49
A Free PVS-Studio License for Open-Source Project Developers 50
END Q&A51

The Use of Static Code Analysis When Teaching or Developing Open-Source Software

  • 1.
    The Use ofStatic Code Analysis When Teaching or Developing Open-Source Software Presenter: George Gribkov
  • 2.
    1. Static analysis:short overview 2. Use of static analysis at colleges and universities 3. Use of static analysis in student and open projects Contents 2
  • 3.
  • 4.
     Write correctcode  Unit tests  Regression testing  Code review  …is there some other way?  Yes! For example – tools for automated analysis. How to Improve Code Quality 4
  • 5.
     Static analysistools: check code when it’s not executed  Dynamic analysis tools: check code when it’s being executed Automated Code Analysis Tools 5  Both approaches compliment each other very well.
  • 6.
    Cost to Fixa Bug 6
  • 7.
     Issues falsepositives  Difficulties with multithreading  Does not eliminate the need for code review Static Analysis Disadvantages 7
  • 8.
     Covers theentire code  Significantly faster than dynamic code analysis  More convenient for large projects Static Analysis Advantages 8
  • 9.
     Can checkcode style or whether the code complies with a coding standard (MISRA, AUTOSAR C++)  Easy to use  Helps developers learn and teach Static Analysis Advantages 9
  • 10.
    Use of StaticAnalysis at Colleges and Universitites 10
  • 11.
     Helps checkhomework  Helps check final projects  Saves instructors’ time For Instructors 11
  • 12.
     Provides achance to learn a new approach  Helps with self-study and problem solving  Facilitates development  Shows and helps study error patterns For Students 12
  • 13.
    Pattern Examples (Vangers) 13 voidaciPackFile(....) { int sz,sz1; char *p,*p1; .... p = new char[sz]; p1 = new char[sz1]; .... delete p; delete p1; }
  • 14.
    Pattern Examples (Vangers) 14 voidaciPackFile(....) { int sz,sz1; char *p,*p1; .... p = new char[sz]; p1 = new char[sz1]; .... delete p; // <= delete p1; // <= }
  • 15.
    Pattern Examples (Vangers) 15 voidaciPackFile(....) { int sz,sz1; char *p,*p1; .... p = new char[sz]; p1 = new char[sz1]; .... delete [] p; delete [] p1; }
  • 16.
    Pattern Examples (ApacheHTTP Server) 16 static void MD4Transform( apr_uint32_t state[4], const unsigned char block[64]) { apr_uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[APR_MD4_DIGESTSIZE]; .... /* Zeroize sensitive information. */ memset(x, 0, sizeof(x)); }
  • 17.
    Pattern Examples (ApacheHTTP Server) 17 static void MD4Transform( apr_uint32_t state[4], const unsigned char block[64]) { apr_uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[APR_MD4_DIGESTSIZE]; .... /* Zeroize sensitive information. */ memset(x, 0, sizeof(x)); // <= }
  • 18.
    Pattern Examples (ApacheHTTP Server) 18 static void MD4Transform( apr_uint32_t state[4], const unsigned char block[64]) { apr_uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[APR_MD4_DIGESTSIZE]; .... /* Zeroize sensitive information. */ memset_s(x, 0, sizeof(x)); } *Or use the following flag: -fno-builtin-memset!
  • 19.
     Provides achance to learn a new approach  Helps with self-study and problem solving  Facilitates development  Shows and helps study error patterns For Students 19
  • 20.
    Use of StaticAnalysis in Student and Open Projects 20
  • 21.
     Static analysisprovides its maximum benefit only when used regularly! Regular Use is the Main Thing 21
  • 22.
    Regular Use isthe Main Thing 22
  • 23.
    Efficient Static Analyzers 23 •PVS-Studio • Clang Static Analyzer • Cppcheck • Infer • IntelliJ IDEA • FindBugs • ... • A detailed list of static analyzers:
  • 24.
    1. A classicdevelopment scenario (in office) 2. Developing student and open-source projects Introducing Analysis 24
  • 25.
     Locally ondevelopers’ computer (plugins for IDE, compilation monitoring system) A Typical Scenario 25
  • 26.
     Continuous integrationsystems (command-line utilities, plugins for CI systems, monitoring systems) A Typical Scenario 26
  • 27.
  • 28.
    What’s the difference? Studentand Open-Source Projects 28
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
    Using an Analyzeron Open-Source Projects 34
  • 35.
    Using an Analyzeron Open-Source Projects 35
  • 36.
    How to AnalyzeCommunity Contribution? 36
  • 37.
    What to DoAfter the First Check? 37
  • 38.
    Using an Analyzeron Open-Source Projects 38
  • 39.
    Using an Analyzeron Open-Source Projects 39
  • 40.
  • 41.
    How to AnalyzeCommunity Contribution? 41
  • 42.
     Suppress basesare a mass suppression tool for the analyzer’s warnings. After the First Check 42
  • 43.
     Suppress basesare a mass suppression tool for the analyzer’s warnings. After the First Check 43
  • 44.
     Hide olderrors – keep up the normal pace  See only the latest warnings starting from this moment  Get immediate benefits from the analyzer  Do not forget about the old errors! Come back and fix them one-by-one. The Purpose of Suppress Bases 44
  • 45.
     A veryconvenient approach: the “ratcheting” method  The number of errors in the base is committed to the repository.  Changes are allowed only when they do not increase the total number of errors. How to Work with Suppress Base 45
  • 46.
    How to Workwith Suppress Base 46
  • 47.
  • 48.
  • 49.
     Static analysishelps study programming  It’s important to use static analysis regularly  It’s okay to use static analysis in open-source projects! Recap 49
  • 50.
    A Free PVS-StudioLicense for Open-Source Project Developers 50
  • 51.