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
4
votes
1 answer

How do I create a module in MISRAC:2012 that follows Dir 4.12 and 4.8?

This question relates to coding in ISO C99 following the MISRAC:2012 guidelines. I am looking for guidance on Dir 4.8 “If a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should…
crisls
  • 43
  • 5
4
votes
1 answer

State Machine with no function pointer

I have implemented a complex state machine with numerous state transitions for a safety SIL 4 system. The back bone for this implementation was done using function pointers. When all was sailing smoothly, the V&V opposed the use of function pointers…
AlphaGoku
  • 968
  • 1
  • 9
  • 24
4
votes
2 answers

Misra violation with bitwise operator

I have written the following piece of code which MISRA does not like: UartPtr->C &= ((uint8_t)(~SIO_C2_SBK)); with #define SIO_C2_SBK ((uint8_t)0x01u) and UartPtr is defined as UartPtr = (UartStruct*) 0x12345678; /* I know that this is also a…
m47h
  • 1,611
  • 2
  • 18
  • 26
4
votes
2 answers

Is Form Feed character (FF) valid in MISRA C2 standard

Opening some legacy code in Notepad++ and notice a few occurrences of FF character below function comment headers. They are ASCII code 12 which is the Form Feed character. Are FF characters valid in MISRA C2 standard please? Apologies I don't…
chris
  • 139
  • 3
4
votes
5 answers

What is a faithful alternative to a linked list in C?

This question might be too broad, or opinion-biased, but I know this site to be full of experienced programmers, and I think it might encourage a good discussion. I am implementing an embedded application in C, in which I use a linked list,…
Michał Szydłowski
  • 3,261
  • 5
  • 33
  • 55
4
votes
1 answer

Static function template and MISRA C++

The following function template with specializations should be used within the same .cpp file only, so I’d like to make it static. The following code compiles (without warnings) using both MS Visual C++ 2008 and GCC 4.8.1 and works as intended.…
Melebius
  • 6,183
  • 4
  • 39
  • 52
4
votes
2 answers

PC-lint on switch case labels: Violates MISRA C++ 2008 Required Rule 5-0-12

//some static const variables are defined static const uint8_t FirstData = 1; static const uint8_t SecondData = 2; //some switch case switch (Numdata) //Numdata is either FirstData, SecondData { case FirstData: //some code case…
4
votes
7 answers

How to resolve this MISRA warning: C++

Here is the code as below: std::stringstream os; os << std::hex; // MISRA warning on this line os << std::setw(2); os << std::setfill('0'); Warning: "Required Rule 8-4-4, function identifier used without '&' or parenthisized parameter…
suhel
  • 321
  • 3
  • 12
4
votes
1 answer

MISRA-C++:2008[8-4-3] : return in all exit path in function

When testing my code (static analysis) to see if i respect misra c++ 2008, i get the following error Function does not return a value on all paths. The function looks like int* Dosomething(string v) { int* retvalue = NULL; if( 0 ==…
bonpiedlaroute
  • 173
  • 1
  • 9
4
votes
3 answers

Literal zero instead of null-pointer-constant warning by MISRA

I have this function: void InitS(unsigned int &numS){ // this function returns a container for unsigned int // but it has a cast for int numS = props.numOfS(); if (numS > 0) { .. } } It compiles but gives me this MISRA…
or.nomore
  • 915
  • 1
  • 10
  • 20
3
votes
1 answer

Avoid throwing exception in C++ assignment-expression of a throw statement (while keeping some sort of readability and maintainability)

TL;DR version How to achieve exception-free C++ assignment-expression in throwing statement without sacrificing code readability and maintainability? Yadi Yada version Goal and current implementation Recently I'm trying to figure out a way to avoid…
user2535650
  • 265
  • 1
  • 2
  • 8
3
votes
0 answers

Is post and pre increment as array index mentioned in Misra?

Is the given example: uint32_t i = 0U; uint32_t arr[5U] = { 0U }; uint32_t b = arr[i++]; Anyhow not compliant with any Misra rules? Can one use pre increment and post increment inside of array indexing? Rule 12.1 is not clear to me about that.
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74
3
votes
1 answer

Pc lint "-egrep" option

I'm trying to clean up my code from misra violations using Pc-lint. One of them is the violation to rule 11.4 in this code. GPIO_PinState level = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6); the definition of GPIOB is #define GPIOB …
buch
  • 33
  • 2
3
votes
1 answer

What changes should I do in the function-like macro to make it MISRA compliant?

The function-like macro is #define ARRAYSIZE(_Array) ((sizeof(_Array)) / (sizeof(_Array[0]))) The error shown is: Error[Pm154]: in the definition of a function-like macro, each instance of a parameter shall be enclosed in parenthesis (MISRA C…
Utkarsh
  • 31
  • 5
3
votes
2 answers

conversion between a pointer to function and another type [MISRA 2012 Rule 11.1, required] | pclint 9074

I am using an array of function pointers as below to avoid a switch statement in the code. void E_func1(void); void E_func2(void); void E_func3(void); void (*pfGetVal[3])() = { E_func1, E_func2, E_func3 }; But while running…
kapilddit
  • 1,729
  • 4
  • 26
  • 51