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

Bitwise operation with (signed) enum value

I am using enumerator values for flags: typedef enum { a = 0x00, b = 0x01u, // the u has no influence, as expected c = 0x02u, // the u has no influence, as expected ... } enum_name; volatile unsigned char* reg = SomeAddress; *reg |=…
lkanab
  • 924
  • 1
  • 7
  • 20
3
votes
1 answer

Why do I have to cast an enum element when assigning it to a same enum variable type in C?

I have the following: typedef enum { FLS_PROG_SUCCESS, FLS_PROG_FAIL, FLS_ERASE_SUCCESS2U, FLS_ERASE_FAIL, FLS_READ_SUCCESS, FLS_READ_FAIL, FLS_FORMAT_SUCCESS, FLS_FORMAT_FAIL }FLS_JobResult_t; void Foo(void) { …
m4l490n
  • 1,592
  • 2
  • 25
  • 46
3
votes
3 answers

Where are Cortex-specific functions like __wfi() defined in Keil?

I'm working on an industrial code for Cortex-M0 controller, using uVision Keil 4.71.0.0 IDE. Our code is supposed to respect MISRA rules and is routinely checked with QA-C. My problem is that some controller-specific functions like __wfi(),…
Dmitry Grigoryev
  • 3,156
  • 1
  • 25
  • 53
3
votes
1 answer

functions shall not be declared at block scope

I couldn't able to understand the following misra rule, " functions shall not be declared at block scope " . the explanation given in the document is "A function declared at block scope will refer to a member of the enclosing namespace, and so the…
Kumar
  • 616
  • 1
  • 18
  • 39
3
votes
2 answers

Popular use of Dynamic memory allocation

I have been reading coding standards in C and most of them discourages use of dynamic memory allocation.But In popular use Dynamic memory allocation leads .Any solid reason for this.I am asking the reasons for its use despite the Demerits it posses…
achoora
  • 1,270
  • 1
  • 16
  • 32
3
votes
2 answers

Misra C error in Macro definition

This piece of code reports three misra c errors: Inappropriate macro expansion Function-like macro definition Macro parameter with no parentheses The original code is: #define Wait(a, b) \ if (READ(b+0x1U)) …
Rog Matthews
  • 3,147
  • 16
  • 37
  • 56
3
votes
2 answers

Why must the first 31 characters of an identifier be unique?

MISRA 2004 rule 5.1 states that all identifiers must have the first 31 characters unique. What is the reason for this rule? Is it a technical limitation with some compilers?
Andreas
  • 7,470
  • 10
  • 51
  • 73
3
votes
4 answers

C++ memory management and Misra

I need some clarification about c++ memory management and MISRA guidelines.. I have to implement one program that it's MISRA compatible so I have to respect a important rule: is not possible to use 'new' operator (dynamic memory heap). In this…
Safari
  • 11,437
  • 24
  • 91
  • 191
2
votes
1 answer

Why inline functions also need prototypes (MISRA C 2012 Rule 8.4)

Let's say there is a C header file: //void test(void); inline void test(void) {} If we comment out the first line, MISRA C checker will complain Type: MISRA C-2012 Declarations and Definitions (MISRA C-2012 Rule 8.4, Required) Triage…
2
votes
1 answer

MISRA C mapping to Klocwork violations

Is there a mapping between MISRA rules for example: A function shall not be declared implicitly (MISRA C R.17.3), such as use "int f(int x);" and not " f(x);". All exit paths from a function with non-void return type shall have an explicit return…
Gilad
  • 6,437
  • 14
  • 61
  • 119
2
votes
0 answers

Autosar C++14 compliant C interface

I have some C++ code with a C interface and I'm struggling to imagine how to come in compliance with a few Autosar C++ rules. For example: std::string GetAddress(const std::string &clientName); vs StatusReturn_t GetAddress(const char *pClientName,…
foreverska
  • 585
  • 3
  • 20
2
votes
2 answers

MISRA C-2012 Rule 10.3 Violation?

When I try to add these macros: #define BOOT_VD_TRUE ((uint8_t)(0x00U)) #define BOOT_VD_FALSE ((uint8_t)(0x01U)) It violates MISRA C-2012 Rule 10.3: Implicit conversion of (uint8_t)1U from essential type "boolean" to different or narrower…
2
votes
4 answers

How to properly do negative value cast to an unsigned type in C?

I want to store a register value and do the bitwise NOT operator like below typedef union TEST_REG { uint32_t u32Register; uint8_t Byte[4]; uint16_t hword[2]; }…
user2986042
  • 1,098
  • 2
  • 16
  • 37
2
votes
2 answers

what does two __pragma inside a define macro means?

I am trying to understand one line of code having two __pragma operatores inside one define macro: #define NULL (_Pragma("nomisrac 10.5") 0 _Pragma("nomisrac restore")) I know that pragma can be used in macros as of the c99 standard. But I only…
Zimo93
  • 31
  • 4
2
votes
1 answer

How to provide #defines to misra checking in cppcheck?

I'm trying to utilize misra C:2012 checking in from cppcheck (v2.9). However, when executing on a particular file I get the following violations: vehicle_controller.c:1494:2: style: All identifiers used in the controlling expression of #if or #elif…
ls6777
  • 386
  • 1
  • 5
  • 14