Versions Compared

Key

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

...

Implementation Details (GCC)

Violating this rule can have  have unexpected consequences, as in the following example:

...

None of the inputs to the function equal the delimiter, so when run with GCC 45.4.3 on Linux, control reaches the end of the getlen() function, which returns 5is undefined behavior and in this test returns 3, causing an out-of-bounds write to the data array.

...

Code Block
bgColor#ccccff
langc
#include <stddef.h>
 
int getlen(const int *input, size_t maxlen, int delim,
           size_t *result) {
  for (size_t i = 0; i < maxlen; ++i) {
    if (input[i] == delim) {
      if (result != NULL) {
        *result = i;
      }
      return 0;
    }
  }
  return -1;
}

void func(int userdata) {
  size_t i;
  int data[] = {1, 1, 1};
  if (getlen(data, sizeof(data), 0, &i) != 0) {
    /* Handle error */
  } else {
    data[i] = userdata;
  }
}

...

ToolVersionCheckerDescription
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
LANG.STRUCT.MRSMissing return statement
LDRA tool suite
Include Page
LDRA_V
LDRA_V
2 D, 36 S, 66 SFully implemented
Parasoft C/C++test9.5MISRA2012-RULE-17_4Fully implemented
PRQA QA-C
Include Page
PRQA QA-C_v
PRQA QA-C_v
2888 
SonarQube C/C++ Plugin3.1S935 

Related Guidelines

...