Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

MISRA C -2023

Prepared by –Chiranjit Adhikary


Software Design Engineer
MISRA C -2023

What is MISRA -C
MISRA C refers to a set of coding guidelines for the C programming language.
• What is MISRA

Motor Industry Software Reliability Association


• Why we need to know about MISRA -C

MISRA C provides rules and recommendations for writing C code in


a structured, consistent, and reliable manner. These guidelines cover
various aspects of C programming, including syntax, semantics, and
language features, with the aim of reducing potential errors and
vulnerabilities in software.
MISRA C -2023

Introduction
MISRA- The Motor Industry Software Reliability Association

Every rule is classified as being either "required” or “advisory”

Required rules: These are mandatory requirements for programmer

Advisory rules: These are some requirements for programmers that


should normally be followed
MISRA C -2023
MISRA C -2023
History of MISRA-C
Improvisation

MISRA C:1998
MISRA C was published in 1998 and remains widely used today. It was written for
C90. There are 127 coding rules, including:

Rule 59
The statement forming the body of an "if", "else if", "else", "while" "do ... while",
or "for" statement shall always be enclosed in braces
MISRA C -2023
History of MISRA-C
Improvisation
Rule 59
Example if(condition ) for(condition)
{ {
// Loop body // Loop body
} }

else if(condition) while(condition)


{ {
// Loop body // Loop body
} }

else do while(condition)
{ {
// Loop body // Loop body
} }
MISRA C -2023

History of MISRA-C
Improvisation

MISRA C:2004
MISRA C:2004 is the second edition of MISRA C, published in 2004. It was
written for C90. There are 142 coding rules, including:

Rule 14.9

An if (expression) construct shall be followed by a compound statement. The else


keyword shall be followed by either a compound statement or another if statement.

Rule 14.10
All if … else if constructs shall be terminated with an else clause.
MISRA C -2023

History of MISRA-C
Improvisation
Rule 14.9
Example
#include <stdio.h>
int main(){
int x = 5; //
/ /Correct usage with another if statement following else
if (x > 10)
{
printf("x is greater than 10\n");
}
else if (x < 0)
{
printf("x is negative\n");
}
else
{
 The else keyword is followed either by a compound statement or another if
printf("x is between 0 and 10\n");
statement.
}
}
MISRA C -2023

History of MISRA-C
Improvisation
Rule 14.10
Example-
#include <stdio.h>
int main() {
int x = 5;
if (x > 0)
{
printf("x is positive\n");
}
else if (x < 0)
{
printf("x is negative\n");
}
else
 The last else clause ensures that there's a default behavior or action to take if
{
none of the previous conditions are met.
printf("x is zero\n");
}
return 0;
}
MISRA C -2023

History of MISRA-C
Improvisation

MISRA C:2012
MISRA C:2012 is the third edition of MISRA C, published in 2012. It was written
for C99 and C90 to provide better rationales for the guidelines and more precise
descriptions. There are 143 rules, including:

Rule 18.1
A pointer resulting from arithmetic on a pointer operand shall address an
element of the same array as that pointer operand
MISRA C -2023

History of MISRA-C
Improvisation

Rule 18.1
Example

int main() {

int arr[5] = {10, 20, 30, 40, 50};

int *ptr = &arr[1]; // Pointer to the second element of the array

// Incrementing the pointer by 1 is valid

ptr++;

printf("Value pointed by incremented pointer: %d\n", *ptr);

return 0;

 Incrementing ptr by 1 (ptr++) is valid since it still points within the same array.
The resulting pointer will point to the third element of the array.
MISRA C -2023

History of MISRA-C
Improvisation

MISRA C:2012 Amendment 1


MISRA C 2012 Amendment 1 was released in 2016. The aim of AMD1 was to add
security guidelines. With this amendment, MISRA C:2012 includes 156 rules and
17 directives for a total of 173 guidelines, including:

Rule 12.5
The size of operator shall not have an operand which is a function parameter
declared as an "array of type"
MISRA C -2023

History of MISRA-C
Improvisation
Rule 12.5
Example
#include <stdio.h>

void func(int arr[]) {


// Incorrect usage violating Rule 18.1
printf("Size of arr in bytes: %zu\n", sizeof(arr));
}

int main() {
int arr[5] = {1, 2, 3, 4, 5};
func(arr);  Inside func, the sizeof operator is used to determine the size of the parameter
arr. However, using sizeof on an array parameter in this manner does not yield
return 0; the size of the array itself but rather the size of a pointer to the array
}
Output =8 // Incorrect
MISRA C -2023

History of MISRA-C
Improvisation

MISRA C:2012 Amendment 2


MISRA C 2012 Amendment 2 was released in 2020 and adds coverage for C11 core
functionality. It adds two new rules. With this amendment, MISRA C:2012 includes
158 rules and 17 directives for a total of 175 guidelines. The new rules are:

Rule 1.4
Emergent language features shall not be used
Rule 21.21
The Standard Library function system() of <stdlib.h> shall not be used
"
MISRA C -2023

History of MISRA-C
Improvisation
Rule 1.4
Example
#include <stdio.h>

// Example of emergent language feature


int main() {
_Bool flag = 1; // Using the _Bool type, which is not standard in C89/C90

if (flag) {
printf("Flag is true\n");
} else {
printf("Flag is false\n");
}

 In this example, _Bool is used as the type for the variable flag.
return 0;
 _Bool is a C99 feature and might not be supported by older compilers or environments.
}
MISRA C -2023

History of MISRA-C
Improvisation

MISRA C:2012 Amendment 3


MISRA C 2012 Amendment 3 was released in 2022 and adds guidance for C11 and C18
new features previously prevented by Rule 1.4. It adds 24 new rules and 1 new directive.
This makes a total of 182 rules and 18 directives, for a total of 200 guidelines.
Additionally, a number of existing guidelines have been revised together with
supporting materials.
The new rules include further guidance on some of the emergent features previously
covered by Rule 1.4, specifically:
MISRA C -2023

Rules 8.15-8.17
Alignment of objects (<stdalign.h>)
The stdalign.h header file defines macros that are associated with
alignment, which is introduced in the C11 (ISO/IEC 9899:2011)
standard: It typically defines macros like alignof and _Alignas:
Rules 17.9-17.13
No-return functions (<stdnoreturn.h>)
In Misra C:2012, rules 17.9 through 17.13 pertain to functions that do
not return to their caller. These rules cover guidelines for using
functions that are marked as not returning (noreturn functions).
MISRA C -2023
Rule 1.5
Obsolescent language features shall not be used
Rule 7.5
Integer-constant macros
Rule 21.24
The random number generator functions of <stdlib.h> shall not be used.

Directive 4.15
Floating point (including comparisons, NaNs, and infinities)

This rule aims to prevent unreliable comparisons of floating-point values


due to issues such as rounding errors, and precision loss. Instead of relying
on exact equality or inequality comparisons for floating-point values,
developers should use alternative approaches such as comparing against a
tolerance threshold.
MISRA C -2023

History of MISRA-C
Improvisation

MISRA C:2012 Amendment 4


Amendment 4 was released in 2023 and completes guidance for C11 and C18 new
features to include multithreading (Rules 22.11 - 22.20) and atomics (updates to
various rules). AMD4 adds 19 new rules and directives, with 221 guidelines total.
MISRA C -2023

History of MISRA-C
Improvisation

MISRA C:2024
A new revision of MISRA C:2012 was published in 2023. It is a rollup of all the
previous amendments and technical corrigenda and is known as MISRA C:2023.
MISRA C:2023 covers C90, C99, and C11/C18. It adds 200 new rules and 21
directives, with 221 guidelines total. MISRA C:2023 is also referred to as MISRA
C Third Edition, second revision.
MISRA C -2023

MISRA C -2023 COMPLIANCE


Matrix
MISRA C -2023 COMPLIANCE MISRA C -2023

Staps
Read the Standard: Obtain a copy of the Misra C:2023 standard document and
thoroughly read through it. Understand the rules, guidelines, and recommendations
provided in the standard.

Select Compliance Level: Misra C standards typically define different compliance


levels, such as "Required", "Advisory", and "Optional". Determine which
compliance level your project needs to adhere to.

Use Static Analysis Tools: Use static code analysis tools that support Misra C
compliance checking. These tools can automatically analyze your codebase and
identify violations of Misra C rules. Tools such as PC-lint, Coverity, and Polyspace
provide Misra C compliance checking features.
MISRA C -2023 COMPLIANCE MISRA C -2023

Staps
Configure Analysis Rules: Configure the static analysis tool to enforce the rules
specified in Misra C:2023. Most tools allow you to customize rule sets based on
your project's requirements and compliance level.

Run Analysis Regularly: Integrate Misra C compliance checking into your


development process by running static analysis regularly, preferably as part of your
automated build process. This ensures that violations are detected early and can be
addressed promptly.

Address Violations: Review the analysis results to identify Misra C violations in


your codebase. Address these violations by modifying the code to comply with the
standard. Document any deviations from the standard and justify them if necessary.
MISRA C -2023 COMPLIANCE MISRA C -2023

Staps
Review and Verify: Conduct code reviews and manual inspections to ensure that
the code complies with Misra C rules. Verify that the implemented solutions
effectively address the identified violations without introducing new issues.
MISRA C -2023 COMPLIANCE MISRA C -2023

Staps
MISRA C -2023
MISRA C -2023

END of MISRA C -
2023
Any ?

You might also like