The three types char
, signed char
, and unsigned char
are collectively called the character types. Compilers have the latitude to define char
to have the same range, representation, and behavior as either signed char
or unsigned char
. Irrespective of the choice made, char
is a separate type from the other two and is not compatible with either.
Only use Use only signed char
and unsigned char
types for the storage and use of numeric values .
Non-Compliant Code Example
because it is the only portable way to guarantee the signedness of the character types (see STR00-C. Represent characters using an appropriate type for more information on representing characters).
Noncompliant Code Example
In this noncompliant code example, the char
-type variable c
may be signed or unsigned. Assuming 8-bit, two's complement character types, this code may print out either i/c = 5
(unsigned) or i/c = -17
(signed). It is much more difficult to reason about the correctness of a program without knowing if these integers are signed or unsigned.This non-compliant code example is taken from an actual vulnerability in bash versions 1.14.6 and earlier that resulted in the release of CERT Advisory CA-1996-22. This vulnerability resulted from the declaration of the string
variable in the yy_string_get()
function as char *
in the parse.y
module of the bash source code:
Code Block | ||
---|---|---|
| ||
static int yy_string_get() {
register char *string;
register int c;
string = bash_input.location.string;
c = EOF;
/* If the string doesn't exist, or is empty, EOF found. */
if (string && *string) {
c = *string++;
bash_input.location.string = string;
}
return (c);
}
|
The string variable is used to traverse the character string containing the command line to be parsed. As characters are retrieved from this pointer, they are stored in a variable of type int
. For compilers in which the char
type defaults to signed char
, this value is sign-extended when assigned to the int
variable. For character code 255 decimal (-1 in two's complement form), this sign extension results in the value -1 being assigned to the integer which is indistinguishable from the EOF
integer constant expression.
Compliant Solution
| ||
char c = 200;
int i = 1000;
printf("i/c = %d\n", i/c);
|
Compliant Solution
In this compliant solution, the variable c
is declared as unsigned char
. The subsequent division operation is now independent of the signedness of char
and consequently has a predictable resultThis problem is easily repaired by explicitly declaring the string
variable as unsigned char
.
Code Block | ||||||
---|---|---|---|---|---|---|
|
static int yy_string_get() {
register unsigned char *string;
register int c;
string = bash_input.location.string;
c = EOF;
/* If the string doesn't exist, or is empty, EOF found. */
if (string && *string) {
c = *string++;
bash_input.location.string = string;
}
return (c);
}
| |||||
unsigned char c = 200;
int i = 1000;
printf("i/c = %d\n", i/c);
|
Exceptions
INT07-C-EX1: void FIO34-C. Use int to capture the return value of character IO functions that might be used to check for end of file mentions that certain character IO functions return a value of type int
. Despite being returned in an arithmetic type, the value is not actually numeric in nature, so it is acceptable to later store the result into a variable of type char
.
Risk Assessment
This is a subtle error that results in a disturbingly broad range of potentially severe vulnerabilities. At the very least, this error can lead to unexpected numerical results on different platforms. Unexpected arithmetic values when applied to arrays or pointers can yield buffer overflows or other invalid memory access.
Recommendation |
---|
Severity | Likelihood | Remediation Cost | Priority | Level | |
---|---|---|---|---|---|
INT07- |
2 (medium)
2 (probable)
2 (medium)
P8
C | Medium | Probable | Medium | P8 | L2 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Astrée |
| Supported indirectly via MISRA C:2012 rules 10.1, 10.3 and 10.4. | |||||||
Axivion Bauhaus Suite |
| CertC-INT07 | |||||||
CodeSonar |
| LANG.TYPE.IOT | Inappropriate operand type | ||||||
Compass/ROSE | Can detect violations of this recommendation. In particular, it flags any instance of a variable of type | ||||||||
| CC2.INT07 | Fully implemented | |||||||
Helix QAC |
| C1292, C1293, C4401, C4421, C4431, C4441, C4451 | |||||||
Klocwork |
| PORTING.SIGNED.CHAR | |||||||
LDRA tool suite |
| 93 S, 96 S, 101 S, 329 S, 432 S, 458 S | Fully implemented | ||||||
Parasoft C/C++test |
| CERT_C-INT07-a | The plain char type shall be used only for the storage and use of character values | ||||||
PC-lint Plus |
| 9112 | Fully supported | ||||||
| Checks for use of plain char type for numeric value (rec. fully covered) | ||||||||
Splint |
| ||||||||
RuleChecker |
| Supported indirectly via MISRA C:2012 rules 10.1, 10.3 and 10.4. | |||||||
SonarQube C/C++ Plugin |
| S820 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Related Guidelines
SEI CERT C++ Coding Standard | VOID INT07-CPP. Use only explicitly signed or unsigned char type for numeric values |
ISO/IEC TR 24772:2013 | Bit Representations [STR] |
MISRA C:2012 | Rule 10.1 (required) Rule 10.3 (required) Rule 10.4 (required) |
MITRE CWE | CWE-682, Incorrect calculation |
...
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.2.5, "Types" Wiki Markup