SlideShare a Scribd company logo
Safety on the Max: How to Write
Reliable C/C++ Code for Embedded
Systems
Presenter:
George Gribkov
 A C++ developer, one of PVS-Studio's
static code analyzer developers.
 Develops a set of diagnostic rules that
check code for compliance with the
MISRA C and MISRA C ++ standards
 gribkov@viva64.com
Presenter: George Gribkov
George Gribkov
2
1.Coding standards: reasons why they
are required
2.MISRA and AUTOSAR: what’s under the hood
3.Standards in your projects
Contents
3
Reasons
4
 Popularity of C
Problems
5
 Popularity of C
 POPULARITY of C
Problems
6
 Popularity of C
 POPULARITY of C
 Popularity of С++
Problems
7
 Popularity of C
 POPULARITY of C
 Popularity of С++
 Imperfections in these languages
Problems
8
 Available compilers
 Standardization
 Portability
 Long use experience
 Efficiency
 Support from analysis tools
What Caused the Popularity
9
 Incomplete standardization
 Undefined, unspecified, implementation-defined
behavior
 Incorrect language use
if ( i = 0 ) or if ( i == 0 )?
Weaknesses of C and C++
10
Weaknesses of C and C++
11
When It Comes to Big Responsibility…
12
 On June 4, 1996, Ariane 5, a European launch vehicle, turned
into confetti on 37th second after liftoff.
A Very Expensive Error
13
 The investigation revealed that the accident was caused by a
programmatic error (an integer overflow).
 The rocket carried 4 satellites.
 The financial losses amounted to 370 000 000 $.
A Very Expensive Error
14
15
It’s time to do something!!!
Coding Standards:
What’s Under the Hood?
16
 MISRA is a set of guidelines
Current versions:
 MISRA C:2012 – 143 rules
 MISRA C++:2008 – 228 rules
MISRA: What Is This?
17
 MISRA means «Motor Industry Software Reliability Association»:
MISRA: What Is This?
18
 Bentley Motor Cars
 Ford Motor Company
 Jaguar Land Rover
 Delphi Diesel Systems
 HORIBA MIRA
 Protean Electric
 Visteon Engineering Services
 The University
of Leeds
 Ricardo UK
 ZF TRW
 AUTOSAR means AUTomotive Open System ARchitecture
A Few Words About AUTOSAR
19
 AUTOSAR means AUTomotive Open System ARchitecture
A Few Words About AUTOSAR
20
 BMW Group
 Bosch
 Continental
 Daimler AG
 Ford
 General Motors
 PSA Peugeot Citroën
 Toyota
 Volkswagen
 …and over 200 more
partners
 AUTOSAR means AUTomotive Open System ARchitecture
 AUTOSAR is a development methodology.
 AUTOSAR C++ is a part of this methodology.
The current version:
 AUTOSAR C++: 19-03 – over 350 rules
A Few Words About AUTOSAR
21
MISRA C++ and AUTOSAR C++
22
MISRA C++ AUTOSAR C++
C++03 ✓ ✓
C++11 ☓ ✓
C++14 ☓ ✓
Industries that Use MISRA and AUTOSAR
23
1.Mandatory – no deviations are permissible
2.Required – deviations are acceptable
3.Advisory – optional to follow
Rule Categories:
24
Mandatory rules:
 Do not use an uninitialized variable’s value
 Do not use a pointer to FILE after the stream is closed
 Do not write unreachable code
 A loop’s counter must not be of a floating-point type
 …
Rule Examples
25
Required rules:
 Do not use goto and longjmp
 Each switch must end with default
 if, else, for, while, do, and switch operator bodies must be
enclosed in braces
 Do not use variadic functions
 …
Rule Examples
26
…and all the rest:
 The ‘L’ suffix must be always capitalized (42L)
 Do not use address arithmetic (except for [] and ++)
 Do not use the ‘comma’ operator
 Do not change a function’s parameter inside the function’s
body
 …
Rule Examples
27
Philosophy
28
There’s a lot!
 Rules are classified according to different criteria
 Rules are applicable to generated code
 A complete list of undefined/unspecified/etc… behaviors
 Check-lists that detail how to set up analyzers, checks etc.
 A matrix that shows intersections with other standards
 Documentation examples
What Else Is There Aside From Rules?
29
Using Standards in Your Projects
30
 Do you check code manually? It
must be a nightmare!
 Use static code analysis tools.
 Static analysis is automated code
review.
Checking Code for Compliance
31
 Start using a standard BEFORE you start a project.
 If you’ve already started your project – think twice.
How to Start
32
 Hide old errors to work at the usual pace.
 This way you will see only warnings for new code.
 You benefit from the analyzer IMMEDIATELY.
 Remember the old errors! Come back and fix them one by
one.
Use Warning Suppression!
33
How to Work with Suppress Base
34
 Locally on each developer’s computer (plugins for IDEs and
compilation monitoring systems are available)
How and When Do You Check Code
35
 Continuous integration systems (command-line utilities,
plugions for CI systems, monitoring systems)
How and Where Can You Check Code
36
How and Where Can You Check Code
37
You need:
 Code that complies with the Mandatory and Required rules;
 A guide enforcement plan;
 Documentation for all deviations;
 Documentation for all warnings from compilers and static
analyzers;
 A guideline compliance summary.
How to Prove Your Project’s Compliance?
38
A sample guide enforcement plan:
A Guide Enforcement Plan
39
Rule Compiler Analyzer Code review
“A” “B” “A” “B”
…
5.1 No errors No errors --- --- Procedure x
5.2 No errors No errors Warning V2561 No messages
…
10.4 Warning 458 No errors No warnings No messages
…
 Sometimes it’s impossible to follow a standard precisely.
Example:
const unsigned char *PORT = 0x10u;
 Different deviations have different specifics.
Document Deviations Well
40
 Deviation documentation must contain:
 The broken rule’s number
 The violation’s location
 Reasons for the deviation
 Safety proof
 Possible consequences
Document Deviations Well
41
A sample guideline compliance summary
A Guideline Compliance Summary
42
Rule
The MISRA
Category
Compliance
…
5.1 Mandatory Compliant
5.2 Required With deviations
…
10.4 Advisory Not used
…
 All C/C++ code complies with Mandatory and Required rules
 The compliance plan is fully filled-out
 All deviations are documented
 All compiler and analyzer warnings are
 The compliance summary is fully filled out
Congratulations! You have set safety to the max!!!
Summary:
43
MISRA Compliance: 2016
Achieving compliance with MISRA Coding Guidelines
More Details on MISRA Standard Compliance
44
1. Remove complex branching, goto and recursion.
2. All loops must have a limit.
3. Give up allocating memory dynamically.
4. Any given function must not exceed a letter-sized
sheet of paper.
5. Use no more than two runtime assertions per
function.
The Power of 10: NASA’s Golden Rules
45
The Power of 10: NASA’s Golden Rules
46
6. Declare data at the lowest scope.
7. Does the function return anything? Do check!
8. Do not use preprocessing.
9. Do not use nested pointers.
10. «A zero-warning rule».
A related article:
The Power of 10: NASA’s Golden Rules
47
Conclusion
48
 Sometimes classic quality assurance methods are
insufficient.
 What do MISRA and AUTOSAR C++ offer?
 Using standards in your code.
Summary
49
END
Q&A50

More Related Content

PPTX
Static Code Analysis for Projects, Built on Unreal Engine
PPTX
PVS-Studio is ready to improve the code of Tizen operating system
PPTX
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PPTX
What has to be paid attention when reviewing code of the library you develop
PPTX
PVS-Studio features overview (2020)
PDF
PVS-Studio advertisement - static analysis of C/C++ code
PPTX
PVS-Studio and static code analysis technique
PPTX
CodeChecker Overview Nov 2019
Static Code Analysis for Projects, Built on Unreal Engine
PVS-Studio is ready to improve the code of Tizen operating system
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
What has to be paid attention when reviewing code of the library you develop
PVS-Studio features overview (2020)
PVS-Studio advertisement - static analysis of C/C++ code
PVS-Studio and static code analysis technique
CodeChecker Overview Nov 2019

What's hot (20)

PDF
MASTER-CLASS: "CODE COVERAGE ON Μ-CONTROLLER" Sebastian Götzinger
PDF
We Continue Exploring Tizen: C# Components Proved to be of High Quality
PDF
Static Code Analysis and Cppcheck
PPTX
Quality assurance of large c++ projects
PDF
Pharo Optimising JIT Internals
PPTX
PVS-Studio, a static analyzer detecting errors in the source code of C/C++/C+...
PDF
Processor Verification Using Open Source Tools and the GCC Regression Test Suite
PPTX
PVS-Studio static analyzer: advanced features
PPTX
IoT 개발자를 위한 Embedded C에서 Test Coverage를 추출해보자
PDF
Pragmatic Code Coverage
PDF
The Little Unicorn That Could
PDF
PVS-Studio for Visual C++
PDF
Hands on clang-format
PDF
I just had to check ICQ project
PDF
Vlsi lab manual exp:2
PDF
The why and how of moving to php 7
PDF
The why and how of moving to php 8
PPT
Virtual platform
PDF
VLSI lab manual Part A, VTU 7the sem KIT-tiptur
PPTX
Static analysis as means of improving code quality
MASTER-CLASS: "CODE COVERAGE ON Μ-CONTROLLER" Sebastian Götzinger
We Continue Exploring Tizen: C# Components Proved to be of High Quality
Static Code Analysis and Cppcheck
Quality assurance of large c++ projects
Pharo Optimising JIT Internals
PVS-Studio, a static analyzer detecting errors in the source code of C/C++/C+...
Processor Verification Using Open Source Tools and the GCC Regression Test Suite
PVS-Studio static analyzer: advanced features
IoT 개발자를 위한 Embedded C에서 Test Coverage를 추출해보자
Pragmatic Code Coverage
The Little Unicorn That Could
PVS-Studio for Visual C++
Hands on clang-format
I just had to check ICQ project
Vlsi lab manual exp:2
The why and how of moving to php 7
The why and how of moving to php 8
Virtual platform
VLSI lab manual Part A, VTU 7the sem KIT-tiptur
Static analysis as means of improving code quality
Ad

Similar to Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems (20)

PPTX
Navigating the jungle of Secure Coding Standards
PDF
What Code Is Deliberately Excluded from Test Coverage and Why? (MSR 2021)
PDF
Security in Embedded systems
PDF
Standard embedded c
PPT
Code coverage
PDF
MISRA C in an ISO 26262 context
PPT
11. Lecture 19 Code standards and review.ppt
PDF
What Is MISRA and how to Cook It
PPTX
Accelerating MISRA and CERT coding standards compliance with dedicated report...
PPTX
An Introduction to MISRA C:2012
PDF
Webinar misra and security
PDF
Achieve iso 26262 certification
PPTX
Zero-bug Software, Mathematically Guaranteed
PDF
Mise en œuvre des méthodes de vérification de modèle et d'analyse statique de...
PDF
Coding Safe Modern C++ With AUTOSAR Guidelines
PPTX
How to improve code quality for iOS apps?
PDF
Getting started with RISC-V verification what's next after compliance testing
PPTX
Static code analyzers as a DevSecOps solution
PDF
[EMC] Source Code Protection
PDF
Report on Advanced Robotics & Programming
Navigating the jungle of Secure Coding Standards
What Code Is Deliberately Excluded from Test Coverage and Why? (MSR 2021)
Security in Embedded systems
Standard embedded c
Code coverage
MISRA C in an ISO 26262 context
11. Lecture 19 Code standards and review.ppt
What Is MISRA and how to Cook It
Accelerating MISRA and CERT coding standards compliance with dedicated report...
An Introduction to MISRA C:2012
Webinar misra and security
Achieve iso 26262 certification
Zero-bug Software, Mathematically Guaranteed
Mise en œuvre des méthodes de vérification de modèle et d'analyse statique de...
Coding Safe Modern C++ With AUTOSAR Guidelines
How to improve code quality for iOS apps?
Getting started with RISC-V verification what's next after compliance testing
Static code analyzers as a DevSecOps solution
[EMC] Source Code Protection
Report on Advanced Robotics & Programming
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
The Great and Mighty C++
PPTX
Static code analysis: what? how? why?
PDF
Zero, one, two, Freddy's coming for you
PDF
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
PDF
PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerab...
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
The Great and Mighty C++
Static code analysis: what? how? why?
Zero, one, two, Freddy's coming for you
PVS-Studio Is Now in Chocolatey: Checking Chocolatey under Azure DevOps
PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerab...

Recently uploaded (20)

PDF
top salesforce developer skills in 2025.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Essential Infomation Tech presentation.pptx
PDF
medical staffing services at VALiNTRY
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
ai tools demonstartion for schools and inter college
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
System and Network Administration Chapter 2
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Transform Your Business with a Software ERP System
PDF
Softaken Excel to vCard Converter Software.pdf
top salesforce developer skills in 2025.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Reimagine Home Health with the Power of Agentic AI​
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Understanding Forklifts - TECH EHS Solution
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
CHAPTER 2 - PM Management and IT Context
L1 - Introduction to python Backend.pptx
Essential Infomation Tech presentation.pptx
medical staffing services at VALiNTRY
How Creative Agencies Leverage Project Management Software.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
ai tools demonstartion for schools and inter college
VVF-Customer-Presentation2025-Ver1.9.pptx
System and Network Administration Chapter 2
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Transform Your Business with a Software ERP System
Softaken Excel to vCard Converter Software.pdf

Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems

  • 1. Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems Presenter: George Gribkov
  • 2.  A C++ developer, one of PVS-Studio's static code analyzer developers.  Develops a set of diagnostic rules that check code for compliance with the MISRA C and MISRA C ++ standards  gribkov@viva64.com Presenter: George Gribkov George Gribkov 2
  • 3. 1.Coding standards: reasons why they are required 2.MISRA and AUTOSAR: what’s under the hood 3.Standards in your projects Contents 3
  • 5.  Popularity of C Problems 5
  • 6.  Popularity of C  POPULARITY of C Problems 6
  • 7.  Popularity of C  POPULARITY of C  Popularity of С++ Problems 7
  • 8.  Popularity of C  POPULARITY of C  Popularity of С++  Imperfections in these languages Problems 8
  • 9.  Available compilers  Standardization  Portability  Long use experience  Efficiency  Support from analysis tools What Caused the Popularity 9
  • 10.  Incomplete standardization  Undefined, unspecified, implementation-defined behavior  Incorrect language use if ( i = 0 ) or if ( i == 0 )? Weaknesses of C and C++ 10
  • 11. Weaknesses of C and C++ 11
  • 12. When It Comes to Big Responsibility… 12
  • 13.  On June 4, 1996, Ariane 5, a European launch vehicle, turned into confetti on 37th second after liftoff. A Very Expensive Error 13
  • 14.  The investigation revealed that the accident was caused by a programmatic error (an integer overflow).  The rocket carried 4 satellites.  The financial losses amounted to 370 000 000 $. A Very Expensive Error 14
  • 15. 15 It’s time to do something!!!
  • 17.  MISRA is a set of guidelines Current versions:  MISRA C:2012 – 143 rules  MISRA C++:2008 – 228 rules MISRA: What Is This? 17
  • 18.  MISRA means «Motor Industry Software Reliability Association»: MISRA: What Is This? 18  Bentley Motor Cars  Ford Motor Company  Jaguar Land Rover  Delphi Diesel Systems  HORIBA MIRA  Protean Electric  Visteon Engineering Services  The University of Leeds  Ricardo UK  ZF TRW
  • 19.  AUTOSAR means AUTomotive Open System ARchitecture A Few Words About AUTOSAR 19
  • 20.  AUTOSAR means AUTomotive Open System ARchitecture A Few Words About AUTOSAR 20  BMW Group  Bosch  Continental  Daimler AG  Ford  General Motors  PSA Peugeot Citroën  Toyota  Volkswagen  …and over 200 more partners
  • 21.  AUTOSAR means AUTomotive Open System ARchitecture  AUTOSAR is a development methodology.  AUTOSAR C++ is a part of this methodology. The current version:  AUTOSAR C++: 19-03 – over 350 rules A Few Words About AUTOSAR 21
  • 22. MISRA C++ and AUTOSAR C++ 22 MISRA C++ AUTOSAR C++ C++03 ✓ ✓ C++11 ☓ ✓ C++14 ☓ ✓
  • 23. Industries that Use MISRA and AUTOSAR 23
  • 24. 1.Mandatory – no deviations are permissible 2.Required – deviations are acceptable 3.Advisory – optional to follow Rule Categories: 24
  • 25. Mandatory rules:  Do not use an uninitialized variable’s value  Do not use a pointer to FILE after the stream is closed  Do not write unreachable code  A loop’s counter must not be of a floating-point type  … Rule Examples 25
  • 26. Required rules:  Do not use goto and longjmp  Each switch must end with default  if, else, for, while, do, and switch operator bodies must be enclosed in braces  Do not use variadic functions  … Rule Examples 26
  • 27. …and all the rest:  The ‘L’ suffix must be always capitalized (42L)  Do not use address arithmetic (except for [] and ++)  Do not use the ‘comma’ operator  Do not change a function’s parameter inside the function’s body  … Rule Examples 27
  • 29. There’s a lot!  Rules are classified according to different criteria  Rules are applicable to generated code  A complete list of undefined/unspecified/etc… behaviors  Check-lists that detail how to set up analyzers, checks etc.  A matrix that shows intersections with other standards  Documentation examples What Else Is There Aside From Rules? 29
  • 30. Using Standards in Your Projects 30
  • 31.  Do you check code manually? It must be a nightmare!  Use static code analysis tools.  Static analysis is automated code review. Checking Code for Compliance 31
  • 32.  Start using a standard BEFORE you start a project.  If you’ve already started your project – think twice. How to Start 32
  • 33.  Hide old errors to work at the usual pace.  This way you will see only warnings for new code.  You benefit from the analyzer IMMEDIATELY.  Remember the old errors! Come back and fix them one by one. Use Warning Suppression! 33
  • 34. How to Work with Suppress Base 34
  • 35.  Locally on each developer’s computer (plugins for IDEs and compilation monitoring systems are available) How and When Do You Check Code 35
  • 36.  Continuous integration systems (command-line utilities, plugions for CI systems, monitoring systems) How and Where Can You Check Code 36
  • 37. How and Where Can You Check Code 37
  • 38. You need:  Code that complies with the Mandatory and Required rules;  A guide enforcement plan;  Documentation for all deviations;  Documentation for all warnings from compilers and static analyzers;  A guideline compliance summary. How to Prove Your Project’s Compliance? 38
  • 39. A sample guide enforcement plan: A Guide Enforcement Plan 39 Rule Compiler Analyzer Code review “A” “B” “A” “B” … 5.1 No errors No errors --- --- Procedure x 5.2 No errors No errors Warning V2561 No messages … 10.4 Warning 458 No errors No warnings No messages …
  • 40.  Sometimes it’s impossible to follow a standard precisely. Example: const unsigned char *PORT = 0x10u;  Different deviations have different specifics. Document Deviations Well 40
  • 41.  Deviation documentation must contain:  The broken rule’s number  The violation’s location  Reasons for the deviation  Safety proof  Possible consequences Document Deviations Well 41
  • 42. A sample guideline compliance summary A Guideline Compliance Summary 42 Rule The MISRA Category Compliance … 5.1 Mandatory Compliant 5.2 Required With deviations … 10.4 Advisory Not used …
  • 43.  All C/C++ code complies with Mandatory and Required rules  The compliance plan is fully filled-out  All deviations are documented  All compiler and analyzer warnings are  The compliance summary is fully filled out Congratulations! You have set safety to the max!!! Summary: 43
  • 44. MISRA Compliance: 2016 Achieving compliance with MISRA Coding Guidelines More Details on MISRA Standard Compliance 44
  • 45. 1. Remove complex branching, goto and recursion. 2. All loops must have a limit. 3. Give up allocating memory dynamically. 4. Any given function must not exceed a letter-sized sheet of paper. 5. Use no more than two runtime assertions per function. The Power of 10: NASA’s Golden Rules 45
  • 46. The Power of 10: NASA’s Golden Rules 46 6. Declare data at the lowest scope. 7. Does the function return anything? Do check! 8. Do not use preprocessing. 9. Do not use nested pointers. 10. «A zero-warning rule».
  • 47. A related article: The Power of 10: NASA’s Golden Rules 47
  • 49.  Sometimes classic quality assurance methods are insufficient.  What do MISRA and AUTOSAR C++ offer?  Using standards in your code. Summary 49