Versions Compared

Key

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

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.

Use only signed char and unsigned char types for the storage and use of numeric values 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.

Code Block
bgColor#FFcccc
langc
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 result.

Code Block
bgColor#ccccff
langc
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-C

Medium

Probable

Medium

P8

L2

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

Supported, but no explicit checker
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
LANG.TYPE.IOTInappropriate operand type
Compass/ROSE



Can detect violations of this recommendation. In particular, it flags any instance of a variable of type char (without a signed or unsigned qualifier) that appears in an arithmetic expression

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.INT07

Fully implemented

Klocwork
Include Page
Klocwork_V
Klocwork_V
PORTING.SIGNED.CHAR
LDRA tool suite
Include Page
LDRA_V
LDRA_V

93 S, 96 S, 101 S, 329 S, 432 S, 458 S

Fully implemented

Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V

CERT_C-INT07-a
CERT_C-INT07-b

The plain char type shall be used only for the storage and use of character values
signed and unsigned char type shall be used only for the storage and use of numeric values

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

Use of plain char type for numerical value

MISRA C:2012 Rule 10.1

MISRA C:2012 Rule 10.3

MISRA C:2012 Rule 10.4

Plain char variable in arithmetic operation without explicit signedness


Operands shall not be of an inappropriate essential type

The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category

Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category

Splint
Include Page
Splint_V
Splint_V



PRQA QA-C
Include Page
PRQA QA-C_v
PRQA QA-C_v

1292, 1293, 4401, 4421, 4431, 4441, 4451


Partially implemented
SonarQube C/C++ Plugin
Include Page
SonarQube C/C++ Plugin_V
SonarQube C/C++ Plugin_V
S820

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

SEI CERT C++ Coding StandardVOID INT07-CPP. Use only explicitly signed or unsigned char type for numeric values
ISO/IEC TR 24772:2013Bit Representations [STR]
MISRA C:2012Rule 10.1 (required)
Rule 10.3 (required)
Rule 10.4 (required)
MITRE CWECWE-682, Incorrect calculation