Professional Documents
Culture Documents
Misrac 2023
Misrac 2023
What is MISRA -C
MISRA C refers to a set of coding guidelines for the C programming language.
• What is MISRA
Introduction
MISRA- The Motor Industry Software Reliability Association
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 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
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() {
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
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>
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
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>
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
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)
History of MISRA-C
Improvisation
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
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.
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.
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 ?