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

Rationale for comment rules in MISRA

Rule 2.2 in MISRA states that "source code shall only use /* ... */ style comments". Does any one know what is the rationale for this rule? what is wrong with // style comments?
Kamyar Souri
  • 1,871
  • 19
  • 29
8
votes
4 answers

Why did my tool throw a MISRA error here?

What can I do to avoid MISRA giving this error for the code below? I tried casting with (unit16_t). But then it didn't allow an explicit conversion. Illegal implicit conversion from underlying MISRA type "unsigned char" to "unsigned int" in complex…
Ammamon
  • 467
  • 1
  • 10
  • 18
8
votes
4 answers

Expression assigned to a wider essential type

I get the following warning from our analysis tool Composite expression assigned to a wider essential type This is the code: uint32_t result; uint8_t resolution; result = 1U << resolution; I tried the following: #define SHIFT_BY_ONE…
homeGrown
  • 375
  • 1
  • 8
  • 25
8
votes
3 answers

MISRA-C error in struct array initialization

I have the following: typedef struct { uint8_t BlockID; uint32_t Copies; uint16_t Size; }NVMM_ConfigType; const NVMM_ConfigType NvmmCnf_Layout[6] = { { 1, 1, 4}, { 2, 3, 4}, { 5, 5, 16}, { 10, 1, 4}, { 11, 2, …
m4l490n
  • 1,592
  • 2
  • 25
  • 46
8
votes
2 answers

Does MISRA C 2012 say not to use bool

I am in the early stages of framing stuff out on a new project. I defined a function with a return type of "bool" I got this output from PC-Lint Including file sockets.h (hdr) bool sock_close(uint8_t socket_id); ^ "LINT: sockets.h (52, 1) Note…
Nick
  • 1,361
  • 1
  • 14
  • 42
8
votes
2 answers

Is MISRA-C applicable to Linux applications

I understand that MISRA-C standards are intended for embedded firmware. When embedded Linux is your product platform, can/should your embedded applications be developed to be MISRA-C compliant? Has anyone ever considered such an exercise? My general…
Andrew W.
  • 81
  • 1
  • 2
8
votes
3 answers

Pure virtual function overridding virtual function

Suppose following code is given. class A { public: virtual void someMethod() { std::cout << "class A" << std::endl; } }; class B : public A { public: ... virtual void someMethod() = 0; ... }; Class B overrides someMethod…
deimus
  • 9,565
  • 12
  • 63
  • 107
8
votes
4 answers

Is there a printf specifier that requires float not double?

I'm getting MISRA type errors when I use "%f" specifier for snprintf with a parameter of type float. According to my research, MISRA is correct because "%f" expectes a type of double. Is there a floating point specifier or modifier that will use…
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
8
votes
3 answers

thoughts on unions in C, with regards to MISRA

Misra says to ban all unions. I also know that deviations are allowed as long as they are discussed and documented thoroughly. We have a microcontroller and an external eeprom to store statistical data (event/error logging, parameter settings and…
Daan Timmer
  • 14,771
  • 6
  • 34
  • 66
7
votes
1 answer

MISRA Violation 12.5 when using && and || in if-statement

I'm working on a project where we are appling MISRA 2004. On most of the violations I got the reason, but one I don't understand: Its in the if-statement with && and || operations. Example: uint8 getValue() { // Some algorithm, simplified with…
7
votes
3 answers

MISRA 10.3 issue about AUTOSAR booleans

In my firm project, the AUTOSAR platform defines booleans like this typedef unsigned char boolean; plus #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif This is not modifiable. Then we get the MISRA 10.3 error…
Oli
  • 71
  • 5
7
votes
2 answers

Is Misra-C compatible with X-macros?

It could be argued that in many cases X-macros increase safety, because it makes easier to make sure that generated arrays are the same length for example. However, Misra C (from 2004 reference) rules seem to have a lot of preprocessor rules that…
user694733
  • 15,208
  • 2
  • 42
  • 68
7
votes
3 answers

MISRA 2012 violation - Type mismatch (Rules 10.1, 10.4)

I'm facing MISRA C 2012 violation that I can't understand. Following is the code: #define I2C_CCRH_FS ((uint8_t)0x80) #define I2C_CCRH_DUTY ((uint8_t)0x40) #define I2C_CCRH_CCR ((uint8_t)0x0F) typedef struct I2C_struct { volatile…
Salahuddin
  • 1,617
  • 3
  • 23
  • 37
7
votes
3 answers

MISRA C++ 2008 Rule 5-2-7 violation: An object with pointer type shall not be converted to an unrelated pointer type, either directly or indirectly

In the following example: void bad_function() { char_t * ptr = 0; // MISRA doesn't complains here, it allows cast of char* to void* pointer void* p2 = ptr; // the following 2 MISRA violations are reported in each of the casts bellow (two…
Baj Mile
  • 750
  • 1
  • 8
  • 17
7
votes
2 answers

Why there shall be no more than one read access with volatile-qualified type within one sequence point?

Given the following code: static volatile float32_t tst_mtr_dutycycle; static volatile uint8_t tst_mtr_direction; static volatile uint32_t tst_mtr_update; void TST_MTR_Task(void) { if (tst_mtr_update == 1U) { tst_mtr_update = 0; …
m4l490n
  • 1,592
  • 2
  • 25
  • 46
1
2
3
28 29