Synopsis: | The termination condition of iterations shall be of type boolean. |
Language: | C |
Severity Level: | 7 |
Category: | Statement and Blocks |
Description: |
Justification Conditions are meant to test boolean values, not integers, characters, pointers or other types. In the latter cases, C will implicitly convert the type into a boolean, which might lead to unintended behavior. Example 1 #include <string.h> static const char *ccbb_s[] = { "Hello", "World", NULL }; static int ccbb_f(void) { int i = 0; bool found = false; while ((ccbb_s[i] != NULL) && !found) /* RIGHT */ { found = (strcasecmp(ccbb_s[i], "World") != 0); i++; } ... Example 2 while (strcasecmp(ccbb_s[i], "World")) /* WRONG: strcasecmp() returns int */ |