Questions tagged [misra]

Use this tag for questions about code that must conform to the coding guidelines called MISRA-C and MISRA-C++.

MISRA Homepage

Document full titles:

  • Guidelines for the use of the C language in critical systems
  • Guidelines for the use of the C++ language in critical systems

Originally written by-and-for the automotive industry, now more widely used, including in the aerospace and defence industries.

Three editions of the C guidelines exist:

  • MISRA-C:1998 - 1st Edition (informally MISRA C1).
    Compatible with C90 only.
  • MISRA-C:2004 - 2nd Edition (informally MISRA C2).
    Compatible with C90 only.
  • MISRA C:2012 - 3rd Edition (informally MISRA C3).
    Released at Embedded World 2013. Compatible with C90 and C99.

An updated MISRA C:2012, 3rd Edition, 1st Revision (informally MISRA C3.1) was released at Embedded World 2019, incorporating Amendment 1 and Technical Corrigendum 1.

MISRA C:2012 Amendment 2 (published February 2020) brings C11 and C17 into scope (albeit with some restrictions).

MISRA C:2012 is the current industry de facto standard and the one recommended to use. The older ones are still available, but not recommended for new projects.

MISRA-C++ only exists in its current revision, MISRA-C++:2008.


Tag usage: Use this tag for all questions related to MISRA C and MISRA C++. It shall always be used together with either the or the tag.

When asking about the MISRA rules, please specify exactly which version you are using: C:1998, C:2004, C:2012 or C++:2008.

421 questions
3
votes
2 answers

What is the correct way to initialize C++ structures with unions?

The MISRA-C++ rule 8-5-2 requires that C++ structures are initialized with the correct braces. I have a structure with unions and I connot find the correct combination of braces to satisfy this rule. I am not sure if my code is incorrect or false…
amanning
  • 31
  • 4
3
votes
1 answer

How to cast unsigned integer to enum using CMSIS API

I am using CMSIS API in my C program for ARM Cortex-M CPU. CMSIS defines NVIC_DisableIRQ function as __STATIC_INLINE void __NVIC_DisableIRQ(IRQn_Type IRQn); IRQn_Type is a typedef enum, like typedef enum IRQn { NonMaskableInt_IRQn =…
ilia
  • 1,082
  • 11
  • 19
3
votes
1 answer

MISRA 5-2-1 rule requires postfix-expressions?

The rule 5-2-1 in MISRA-2008 states that Each operand of a logical && or || shall be a postfix-expression. Exception: Where an expression consists of either a sequence of only logical && or a sequence of only logical ||, extra parentheses are not…
john c. j.
  • 725
  • 5
  • 28
  • 81
3
votes
3 answers

A cast should not be performed between a pointer type and an integral type

I am performing a pointer assignment in an Embedded C script like so: uint32_T *a = (uint32_T *) (4096U); Basically, I need a to point to the address location 4096 (decimal) I get the MISRA warning as specified in the title (I use Code Composer…
3
votes
4 answers

Is it legal write to a byte array in a union and read from an int to convert values in MISRA C?

I guess this must have been asked before, but I could not get a specific yes/no answer. I have this code snippet : union integer_to_byte { signed int IntPart; unsigned char BytePart[2]; }; typedef union integer_to_byte I2B; main() { …
EmbSysDev
  • 131
  • 4
3
votes
3 answers

C struct information hiding (Opaque pointer)

I'm currently a bit confused regarding the concept of information hiding of C-structs. The backround of this question is an embedded c project with nearly zero knowledge of OOP. Up until now I always declared my typedef structs inside the header…
Evox402
  • 111
  • 1
  • 13
3
votes
1 answer

Avoiding exceptions in default parameters

I have a function declaration like this: void foo(std::string const &bar = std::string("")); Now, we are using MISRA C++, and our code verifier gives an error on the default value's constructor: RULE 15-5-3: The terminate() function shall not be…
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
3
votes
0 answers

Which C++ standard library functions allocate memory?

It's obvious that the collection classes with their class Allocator = std::allocator parameter explicitly use heap allocations, but which other library functions (or indeed language features) can be expected to allocate memory in a practical…
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
3
votes
3 answers

Use of #define to alias structure members

This is a subjective question, so I will accept 'there is no answer' but read fully as this is specifically on a system where the code is safety critical. I've adopted some embedded C code for a safety critical system, where the original author has…
Jay M
  • 3,736
  • 1
  • 24
  • 33
3
votes
1 answer

Unpermitted operand to operator '>>' and '&' (MISRA C)

When checking some code with Misra, it has generated following messages Unpermitted operand to operator '>>' [MISRA 2012 Rule 10.1 required] Unpermitted operand to operator '&' [MISRA 2012 Rule 10.1 required] I wasn't able to understand the issue,…
user145760
  • 124
  • 9
3
votes
1 answer

How to resolve MISRA C:2012 Rule 13.2 and 13.3 for C code?

I have C source code and I am making it MISRA Compliant. I got an following errors related to MISRA 2012 Rule 13.3 and 13.2: increment/decrement operation combined with other operation with side-effects [MISRA 2012 Rule 13.3, advisory]buf[count++]…
raj123
  • 564
  • 2
  • 10
  • 27
3
votes
2 answers

Is declaring a variable inside an if statement in c a bad habit?

My assumption is that this is going to mess with checkers and stack analysis. I can't prove my assumption and I don't think C99 will complain. Probably neither c89 will because the definition is immediately after the opening of the curly brace: …
Catosh
  • 163
  • 1
  • 2
  • 14
3
votes
2 answers

Is it possible to disable MISRA checks of external libraries?

Is it possible to disable MISRA checks of external libraries? I've tried this, but it doesn't seem to work (this header uses C++ style comments and incompatible @ tags). #pragma ghs startnomisra #include #pragma ghs endnomisra Update…
Minor Threat
  • 2,025
  • 1
  • 18
  • 32
3
votes
3 answers

MISRA C:2004 10.5 Query

I am somewhat stucked at a part of the code that gives MISRA C 2004 10.5 violation but am not able to figure out what exactly might be the cause. I have defined this MACRO to get the power of 2. The code which shows the violation is #define…
Ankit Shah
  • 97
  • 1
  • 11
3
votes
1 answer

MISRA 2012 rule 8.10 static inline

Why does MISRA recommends that an inline function be declared with static storage class? While the keyword inline is a hint to the compiler to replace all function calls with the actual function body, and the compiler may or may not perform it, how…
Girish Onte
  • 107
  • 2
  • 8