Main Content

MISRA C:2023 Rule 1.3

There shall be no occurrence of undefined or critical unspecified behaviour

Since R2024a

Description

Rule Definition

There shall be no occurrence of undefined or critical unspecified behaviour.

Rationale

C code that results in undefined or critical unspecified behavior might produce unexpected or incorrect results. Such code might behave differently in different implementations. Issues caused by undefined behavior in the code might be difficult to analyze because compilers might optimize the code assuming that undefined behavior does not occur.

Note

Many MISRA C:2012 rules address specific undefined or critical unspecified behaviors. This rule applies to any undefined or critical unspecified behavior that is not addressed in any other rule.

Polyspace Implementation

Polyspace® flags these instances of undefined or critical undefined behavior:

  • Use of offsetof on bit fields.

  • Use of offsetof when the second argument is not a struct field of the first argument.

  • Use of defined without an identifier.

  • Use of an array of incomplete types.

  • Use of a function like macros by using incorrect number of arguments (C90 only).

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

        #include <stddef.h>     
static int bar = 0;
extern int bar;         /* Violation (8.8)*/

struct str {
  char a:8;
  char b[10];
  char c;
};
void foo() {
  
  offsetof(struct str, a);//Noncompliant
  offsetof(struct str, d);//Noncompliant
}

In this example, the function foo uses the macro offsetof on the bit field str.a. This behavior is undefined. Polyspace flags it. The function then calls offsetof on str.d. Because d is not a field of str, Polyspace flags it. These issues might cause compilation errors.

The variable bar is declared with both internal and external linkage. According to the C99 standard, declaring a variable to have both internal and external storage in the same file is undefined behavior. Polyspace flags this undefined behavior as a violation of rule 8.8. See MISRA C:2012 Rule 8.8.

Check Information

Group: Standard C Environment
Category: Required
AGC Category: Required

Version History

Introduced in R2024a