Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

C99 defines undefined behavior in Section 3.4.3 as:

behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

...

Behavior can be classified as undefined by the C standards committee for a variety of the following reasons including::

  • to give the implementor license not to catch certain program errors that are
  • The behavior is erroneous.
  • The behavior is difficult to diagnose.As
  • a hook for implementation extensionsto identify areas of possible conforming language extension: the implementor may augment the language by providing a definition of the officially undefined behavior.

Conforming implementations can deal with undefined behavior in a variety of fashions, from ignoring the situation completely with unpredictable results, to translating or executing the program in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Because compilers are not obligated to generate code for undefined behavior, these behaviors are candidates for optimization. By assuming that undefined behaviors will not occur, compilers can generate code with better performance characteristics.

...

Code Block
bgColor#ccccff
#include <assert.h>

int foo(int a) {
  assert(a < (INT_MAX - 100));
  printf("%d %d\n", a + 100, a);
  return a;
}

int main(void) {
  foo(100);
  foo(INT_MAX);
}

Risk Assessment

Depending on undefined behavior leads to problemsWhile it is rare that the entire application can be strictly conforming, the goal should be that almost all the code is allowed for a strictly conforming program (which among other things means that it avoids undefined behavior), with the implementation-dependent parts confined to modules that the programmer knows he needs to adapt to the platform when it changes.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

MSC15-A

high

likely

medium

P18

L1

...