CertC-STR04ΒΆ
Use plain char for characters in the basic character set
Required inputs: IR
There are three character types:
char,
signed char, and
unsigned char. 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.
For characters in the basic character set, it does not matter which data
type is used, except for type compatibility. Consequently, it is best to use
plain
char for character data for compatibility with standard
string-handling functions.
In most cases, the only portable operators on plain
char types are assignment and equality operators (
=,
==,
!= ). An exception is the translation to and from digits. For
example, if the
char
c is a digit,
c - '0' is a value between 0 and 9.
Noncompliant Code Example
This noncompliant code example simply shows the standard string-handling
function
strlen() being called with a plain character string, a signed
character string, and an unsigned character string. The
strlen() function takes a single argument of type
const char *:
size_t len; char cstr[] = "char string"; signed char scstr[] = "signed char string"; unsigned char ucstr[] = "unsigned char string"; len = strlen(cstr); len = strlen(scstr); /* Warns when char is unsigned */ len = strlen(ucstr); /* Warns when char is signed */
Compiling at high warning levels in compliance with MSC00-C. Compile cleanly at high warning levels causes warnings to be issued when
- Converting from
unsigned char[]toconst char *whencharis signed - Converting from
signed char[]toconst char *whencharis defined to be unsigned
Casts are required to eliminate these warnings, but excessive casts can make code difficult to read and hide legitimate warning messages.
If this C code were compiled using a C++ compiler, conversions from
unsigned char[] to
const char * and from
signed char[] to
const char * would be flagged as errors requiring casts.
Compliant Solution
The compliant solution uses plain
char for character data:
size_t len; char cstr[] = "char string"; len = strlen(cstr);
Conversions are not required, and the code compiles cleanly at high warning levels without casts.
Risk Assessment
Failing to use plain
char for characters in the basic character set can lead to
excessive casts and less effective compiler diagnostics.
| Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
| STR04-C | Low | Unlikely | Low | P3 | L3 |
Related Guidelines
| SEI CERT C++ Coding Standard | VOID STR04-CPP. Use plain char for characters in the basic character set |
| MISRA C:2012 | Rule 10.1 (required) Rule 10.2 (required) Rule 10.3 (required) Rule 10.4 (required) |
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
numeric_char_used_as_character |
Signed/unsigned char datatype used for character data. |
None |
False |
Options
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.