Misra C
Misra C
standards
Introduction
• MISRA- The Motor Industry Software Reliability
Association
• Every rule is classified as being either “required”
or “advisory”
• Required rules: These are mandatory
requirements placed on the programmer
• Advisory rules: These are requirements placed
on the programmer that should normally be
followed
09/20/2022 Embedded systems & IoT 2
Presentation of rules
• Rule <number> (<category>): <requirement text>
<number> Every rule has a unique number. This number
consists of a rule group prefix and a group member
suffix.
<category> is one of “required” or “advisory”,
<requirement text> The rule itself.
• <source ref> This indicates the primary source(s) where a
rule originates from one or more published sources
these are indicated in square brackets after the rule.
Rule 1.1 (req) [IEC 61508 Part 3]
09/20/2022 Embedded systems & IoT 3
typedef’d types
• char_t plain 8 bit character
• uint8_t unsigned 8 bit integer
• uint16_t unsigned 16 bit integer
• uint32_t unsigned 32 bit integer
• int8_t signed 8 bit integer
• int16_t signed 16 bit integer
• int32_t signed 32 bit integer
• float32_t 32 bit floating-point
• float64_t 64 bit floating-point
09/20/2022 Embedded systems & IoT 4
Environment
Rule 1.3 (req) Multiple compilers and/or languages shall only be used
if there is a common defined interface standard for object code to
which the languages/compilers/ assemblers conform.
e.g. ELF (Extensible Linking Format) format - Executable and Linkable Format
• Rule 2.2 (req) Source code shall only use /* … */ style comments.
• Rule 2.3 (req) The character sequence /* shall not be used within a
comment.
The double-slash comments (//) expire at the end of the
line. Slash-star (/*) comments are in effect until a closing comment
mark (*/)
struct gnd_speed
{
uint16_t speed; /* mph */
/* Not Compliant - speed is in different units */
} * y;
x->speed = y->speed;
L3 P1-P4
#include <stdlib.h>
int main(void)
{
for (size_t i = 0; i < 100; ++i)
{
char *ptr = (char *)malloc(0x10000000);
*ptr = 'a';
}
return 0;
}
09/20/2022 Embedded systems & IoT 79
Noncompliant Code Example (Implicit
Return Type)
#include <limits.h>
#include <stdio.h>
foo(void)
{
return UINT_MAX;
}
int main(void)
{
long long int c = foo();
printf("%lld\n", c);
return 0;
}
Do not declare a function with an implicit return type UINT_MAX is incorrectly
converted to −1
#ifndef MY_HEADER_H
#define MY_HEADER_H
#endif
switch (expr)
{ case 0: i = 17;
/* Falls through into default code */
default: printf(".oe%d\n"., i);
}
return 0;
}
#include <tgmath.h>
void func(void)
{ double complex c = 2.0 + 4.0 * I;
double complex result = log(c)/log(2);
}
#include <limits.h>
void func(unsigned int ui_a, unsigned int ui_b) {
unsigned int usum;
if (UINT_MAX - ui_a < ui_b) { /* Handle error */ }
else
{ usum = ui_a + ui_b; }/* ... */ }
#include <limits.h>
void func(void)
{ unsigned long int u_a = ULONG_MAX;
signed char sc;
if (u_a <= SCHAR_MAX)
{ sc = (signed char)u_a; /* Cast eliminates warning */
}
else { /* Handle error */
}
}
09/20/2022 Embedded systems & IoT 120
INT32-C. Ensure that operations on signed
integers do not result in overflow
Noncompliant Code Example
void func(signed int si_a, signed int si_b)
{
signed int sum = si_a + si_b; /* ... */
}