Versions Compared

Key

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

The There are three character 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 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.

The only permissible In most cases, the only portable operators on plain char types are assignment and equality operators (=, ==, != ).

Non-Compliant Code Example

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-The following non-compliant code example simply illustrates calling 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 *:

Code Block
bgColor#FFCCCC
langc

#include <string.h>

int main(void) {
  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);  /* warnsWarns when char is unsigned */
  len = strlen(ucstr);  /* warnsWarns when char is signed */
  return 0;
}

Wiki MarkupCompiling at high warning levels in compliance with [MSC00-AC. Compile cleanly at high warning levels] causes warnings to be issued when converting from {{unsigned char\[\]}} to {{char const *}} when {{char}} is signed and from {{signed char\[\]}} to {{char const *}} when char is 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.

  • Converting from unsigned char[] to const char * when char is signed
  • Converting from signed char[] to const char * when char is 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 Wiki MarkupIf this C code were to compiled using a C+\+ compiler, conversions from {{unsigned char\[\]}} to {{char const *}} and from {{signed char\[\]}} to {{char const *}} would be be flagged as errors requiring casts.

Compliant Solution

The compliant solution uses plain char for character data.:

Code Block
bgColor#ccccff
langc

#include <string.h>

int main(void) {
  size_t len;
  char cstr[] = "char string";

  len = strlen(cstr);
  return 0;
}

...

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

STR07

STR04-

A

1 (low)

1 (unlikely)

2 (medium)

P2

L3

Related Vulnerabilities

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

References

Wiki Markup
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.2.5, "Types"
\[[MISRA 04|AA. C References#MISRA 04]\] Rule 6.1, "The plain char type shall be used only for the storage and use of character values"

C

Low

Unlikely

Low

P3

L3

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

Supported indirectly via MISRA C:2004 rule 6.1.
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-STR04
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
LANG.TYPE.IAT
LANG.TYPE.ICA
LANG.TYPE.IOT
LANG.TYPE.MOT
Inappropriate assignment type
Inappropriate character arithmetic
Inappropriate operand type
Mismatched operand types
Compass/ROSE




ECLAIR
Include Page
ECLAIR_V
ECLAIR_V

CC2.STR04

Fully implemented

EDG




Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C0432, C0674, C0699
LDRA tool suite
Include Page
LDRA_V
LDRA_V
93 S, 101 S, 329 S, 432 S, 458 SPartially implemented
Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V
CERT_C-STR04-a

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

RuleChecker
Include Page
RuleChecker_V
RuleChecker_V

Supported indirectly via MISRA C:2004 rule 6.1.
SonarQube C/C++ Plugin
Include Page
SonarQube C/C++ Plugin_V
SonarQube C/C++ Plugin_V
S810

Related Vulnerabilities

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

Related Guidelines

SEI CERT C++ Coding StandardVOID 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)


...

Image Added Image Added STR06-A. Don't assume that strtok() leaves the parse string unchanged      07. Characters and Strings (STR)       Image Modified