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
7
votes
3 answers

How to make (1 << 9) pass MISRA?

We are using Parasoft Static Analysis with MISRA C 2004 checker turned on. The software is an embedded system. We like to describe constants as follows: [1] #define MOTOR_ON (1 << 9) This would show that the 9th bit in the register should…
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
7
votes
5 answers

Is there a way to make an enum unsigned in the C90 standard? (MISRA-C 2004 compliant)

I'm trying to find a way to make an enum "unsigned". enum{ x1 = 0, x2, x3 }; uint8_t = x2; /* <--- PC-LINT MISRA-C 2004 will complain about mixing signed and unsigned here */ Of course, I can add a typecast to get rid of the error,…
Tom
  • 776
  • 1
  • 5
  • 12
7
votes
3 answers

Why do the MISRA rules prohibit the use of '#undef'?

Why do the MISRA rules prohibit the use of #undef in a program? If I want to limit the scope of any macro, how to do it without using #undef?
bubble
  • 3,408
  • 5
  • 29
  • 51
6
votes
3 answers

Which form is better for return value of functions in embedded C?

I am working on embedded C. Could somebody help me which piece of code? Is efficient in terms of robustness, memory as well as Misra friendly? Code1: if (func() == 1 || func() == 2) { /* Body of the function */ } Code2: locvar = func(); if…
6
votes
3 answers

How to use setters properly when the object to be accessed is encapsulated more than once?

I struggle with this question very often and couldn't find any clear solution. I think I know the motivation of getters/setters. Prior Information: When realizing real life data, usually the data is encapsulated in more than one layers. For…
bomberman
  • 142
  • 10
6
votes
2 answers

MISRA-C coding guidelines for personal use programs?

I am usually a wood worker and not a developer. I'm learning C/C++ for embedded systems while trying to make some of my tool autonomous to save me hours of repetitive work. For now, its fun and going well, I have spend maybe a hundred of hours…
A.albin
  • 274
  • 2
  • 15
6
votes
1 answer

Does size_t foo = 0; need a cast?

Looking at this answer and knowing that 0 is an octal constant: For hexadecimal [constants [and octal according to the comments]], it is the first type the value can fit in: int, unsigned int, long, unsigned long, long long, unsigned long…
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
6
votes
1 answer

c++ Digraphs should not be used (MISRA C++ 2-5-1)

According to MISRA C++ 2-5-1 we should generally avoid messing up with digraphs. Though, I don't understand why we should also avoid the use of readable words and, or, not etc. to define common operators &&, ||, ... The issue is even highlighted as…
Alex Gidan
  • 2,619
  • 17
  • 29
6
votes
4 answers

MISRA equivalent for medical industry?

Pretty much as the title, is there a medical industry equivalent document, standard or set of either to the auto industry's MISRA? I'm working in the area of C, but anything language agnostic would be good too
Toby
  • 9,696
  • 16
  • 68
  • 132
5
votes
7 answers

MISRA C 2012 Rule 15.4 and replacing goto's with break's

Regarding to MISRA C 2012 rule 15.4 - "There should be no more than one break or goto statement used to terminate any iteration statement." - is this example correct? Can anyone confirm this with some tool (MISRA checker)? do { retval =…
5
votes
1 answer

Why MISRA-C disallow implicitly widening a type in some circumstances?

Implicitly widening the type of a function argument or a return expression is disallowed by MISRA-C:2004 Rule 10.1, as illustrated in the following code snippet: void foo1(int16_t x); int16_t foo2(void) { int8_t s8a; ... foo1(s8a); …
dingcurie
  • 121
  • 6
5
votes
4 answers

MISRA C:2012 Rule 14.4

As per the MISRA rule The controlling expression of an if statement and the controlling expression of an iteration-statement shall have essentially Boolean type #include #include void foo(void){ int i = 0; if(i){} …
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
5
votes
1 answer

Is there a MISRA-compliant way to use enum flags in C99?

I have a project I'm developing in C99 and I'm trying to make it compliant with the MISRA 2012 standard. In one file I define an enum where each value should be treated as a flag: /** * Enumerates the configurable options for performing…
Tagc
  • 8,736
  • 7
  • 61
  • 114
5
votes
3 answers

Bypass the warning: "Control variable is not modified inside the loop"

I have a very simple function which created a time delay: void delay(int time_in_ms) { int t = get_time() + time_in_ms; while (get_time() < t) { }; } delay(750); I'm getting the warning that the control variable t is not modified…
JohnDoe
  • 825
  • 1
  • 13
  • 31
5
votes
3 answers

Should a function prototype always be in its header file?

Lets say we have a few C source files such as file1.c, file2.c and main.c. We have functions as: file1.c |---> file1Func1() |---> file1Func2() file2.c |---> file2Func1() |---> file2Func2() and the main file uses these…
Osaid
  • 557
  • 1
  • 8
  • 23
1 2
3
28 29