
...
However, this rule is applicable only in only in cases where the character data may contain values that can be interpreted misinterpreted as negative numbers. For example, if the char
type is represented by a two's complement 8-bit value, any character value greater than +127 is interpreted as a negative value.
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <limits.h> #include <stddef.h> static const char table[UCHAR_MAX + 1] = { 'a' /* ... */ }; ptrdiff_t first_not_in_table(const char *c_str) { for (const char *s = c_str; *s; ++s) { if (table[(unsigned char)*s] != *s) { return s - c_str; } } return -1; } |
Exceptions
STR34-C-EX1: This rule only applies to characters that are to be treated as unsigned chars for some purpose, such as being passed to the isdigit() function. Characters that hold small integer values for mathematical purposes need not comply with this rule.
Risk Assessment
Conversion of character data resulting in a value in excess of UCHAR_MAX
is an often-missed error that can result in a disturbingly broad range of potentially severe vulnerabilities.
...