...
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 | ||||
---|---|---|---|---|
| ||||
#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;
}
}
|
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
CodeSonar |
| LANG.STRUCT.MRS | Missing return statement | ||||||
LDRA tool suite |
| 2 D, 36 S, 66 S | Fully implemented | ||||||
Parasoft C/C++test | 9.5 | MISRA2012-RULE-17_4 | Fully implemented | ||||||
PRQA QA-C |
| 2888 | |||||||
SonarQube C/C++ Plugin | 3.1 | S935 |
Related Guidelines
...