What is MISRA C
Learn here how following MISRA guidelines can help you build portable, reliable and easier to maintain C code
MISRA is a word often thrown around in the automotive industry, especially in when projects start coming close to completion. This is the first mistake, and by the end of this article, we hope you will understand why. First, let's us understand what MISRA means and how it has evolved throughout the years.
MISRA is a consortium that provides guidelines to develop software related to safety and security-minded systems. Being compliant to MISRA means your code is more trustworthy, as the end product will be robust, more portable and easier to maintain. Sounds pretty good, right? The MISRA acronym comes from "Motor Industry Software Reliability Association", and although it sounds much like automotive, MISRA is nowadays transversal to many sectors, which caught on with its potential. In essence, MISRA defines a set of guidelines, which are subdivided into rules and directives, where:
- Rule - guideline which contains a complete description of the requirement, in order to check for compliance with it
- Directive - guideline for which is not possible to provide the full description, necessary to assert the compliance
The first edition of MISRA C guidelines were introduced in 1998, but since then there have been a few updates. One of the most recent is the second amendment to MISRA C:2012, which was published in february 2020, containing 175 guidelines, from which there are 16 mandatory rules, 120 required rules and 39 are advisory. The differences between these 3 concepts are:
- Mandatory - necessary to achieve MISRA compliance
- Required - if they cannot be fulfilled, then they should be subject to formal deviation. Formal deviations are considered "giant time and money savers", as they allow you to not comply with a specific rule, if the formal deviation is agreed upon by both parties involved in the project
- Advisory - should be complied with when practical
The MISRA C rules are be defined by the following categories:
- A Standard C Environment
- Unused Code
- Comments
- Character Sets and Lexical Conversions
- Identifiers
- Types
- Literals and Constants
- Declarations and Definitions
- Initialization
- The essential type model
- Pointer type conversions
- Expressions
- Side effects
- Control statement expressions
- Control flow
- Switch statements
- Functions
- Pointers and arrays
- Overlapping storage
- Preprocessing directives
- Standard libraries
- Resources
Now, we can already see why static code analysis is a big thing. In the further parts of this article, we will explore each subset of these rules, and by the end, our crash course in MISRA C will be complete and we will all be pros!
Author: Micael Coutinho (opens in a new tab)
References:
© AutosarToday —@LinkedIn