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
2
votes
6 answers

Can a program fix itself (Variables)? (safety critical enviroment)

I just got started into writing fail-safe, high integrity C code and I'd like to know if programs can "fix themselves" if a variable gets corrupted for whatever reason (for example cosmic rays). I know that there's specific hardware like ECC ram…
2
votes
4 answers

MISRA2012 Rule 12.1 Extra parentheses recommended

I'm trying to fix the compliance of my code to misra C. During the static analysis, I had this violation: Rule 12.1: Extra parentheses recommended. A conditional operation is the operand of another conditional operator. The code is: if…
2
votes
4 answers

MISRA-C: Is it safe to cast a char array to a structure pointer?

When I have a user defined type like the following: typedef struct MyData_t { uint16_t val; ... } MyData; And a simple array that I want to use to store different types of structures in: uint8_t buffer[]; And I then want to create a structure…
Daniel
  • 403
  • 2
  • 15
2
votes
2 answers

MISRA 5-0-15 - Pointer Arithmetic - Rule Violation

The following code violates the MISRA C++ rule 5-0-15: Array indexing shall be the only form of pointer arithmetic. (1) void doSomething(const uint8_t *&ptr, size_t num) { ptr += num; } Incrementing any pointer also violates the above…
KplnMoon
  • 151
  • 1
  • 10
2
votes
3 answers

How to properly solve memset() function MISRA errors in C?

I have written a simple function to initialise the structure values using memset(). These are the code I have written in C language. myfile.h typedef struct{ bool flag; bool check; int val_1; int val_2; } MY_STRUCT; myfile.c static MY_STRUCT…
user2986042
  • 1,098
  • 2
  • 16
  • 37
2
votes
2 answers

MISRA C2012:10.8 violation in the sample code

typedef struct{ sint16 temperature; uint32 Setdata; } VariableA; VariableA TableData[N]; static uint16 linearinterpolation(const currentdata *pcurData,const VariableA* pTableData) { /* Declare local variables */ sint32 deltaOut; …
Rickykidd
  • 23
  • 5
2
votes
2 answers

Using the stringify operator '#' MISRA Rule 20.10 (MISRA C:2012) .What are the alternative way to implement the stringify macro

MISRA standard doesn't allow to use the stringify operator in macro definition What is the alternate way to implement the same concept without using # operator?
DJellybean
  • 33
  • 3
2
votes
1 answer

MISRA warning when overriding bitwise operator

I wrote a simple wrapper for the logging interface, so I can use left-shift operator<< to print log. logger_wrapper.h class LoggerWrapper { public: LoggerWrapper(logging::LoggerInterface *logger, logging::LogPriority priority); …
Tran Ngu Dang
  • 2,540
  • 6
  • 29
  • 38
2
votes
2 answers

The right hand operand of a logical operator || has persistent side effects because of calling function (MISRA- C 2012Rule 13.5)

The right hand operand of a logical operator || has persistent side effects because of calling function detectError(). if ( ( detect() == VALID ) || ( detectError() == INVALID ) ) { up( a,b ); } typedef…
2
votes
3 answers

For loop should be well-formed

MISRA C-2012 Control Flow Expressions (MISRA C-2012 Rule 14.2) misra_c_2012_rule_14_2_violation: The expression i used in the for loop clauses is modified in the loop body. for( i = 0; i < FLASH; i++ ) { if( name.see[i] == 0xFF ) { …
2
votes
1 answer

Misra warning C code - Comparison of boolean and unsigned values in if loop

In the below C code, while checking the if condition, i am getting the Misra warning as The operand of the opeartor '=='do not have same essential type category: one is 'Boolean' and other is 'unsigned' In the header file file1.h #define timer_4sec…
user2986042
  • 1,098
  • 2
  • 16
  • 37
2
votes
2 answers

Safe coding practices

I'm starting a new C/C++ embedded app and am trying to educate myself about safe coding practices like MISRA, AUTOSAR and my current favorite probably because it's the shortest, NASA's so-called Power of 10…
Gene
  • 47
  • 7
2
votes
1 answer

__asm and PCLint 9.0L Error 14: Symbol 'TS_IntDisableAsm(void)' previously defined

I using the PCLint-Check 9.0L for a Project and got the current Error Message during a Lint-Check on all Lint-Object-Files(*.lob) of the Project: W:\DevWA\src\Platforms_h\TSPlatforms.h Error 14: Symbol 'TS_IntDisableAsm(void)' previously defined…
HoloJens
  • 67
  • 9
2
votes
1 answer

Misra and bit operations

I have some understanding problems of misra and bitwise operations. I have following operation: ((in >> bit) & 1u) Here in has the type unsigned short and bit has the type int. 1u should be an unsigned int in my understanding. First I dont…
HS94
  • 23
  • 3
2
votes
1 answer

Does MISRA check if array index out of bounds?

In the MISRA-C standard 2012 I could not find an explicit rule that says that the implementer needs to check that the array is not accessed with an index out of bounds. So an array out of index / boundaries could be there. Maybe this is nothing…
Peter
  • 1,629
  • 2
  • 25
  • 45