You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 23 Next »

According to C99, Section 5.2.1, "Character sets"

Two sets of characters and their associated collating sequences shall be defined: the set in which source files are written (the source character set), and the set interpreted in the execution environment (the execution character set). Each set is further divided into a basic character set, whose contents are given by this subclause, and a set of zero or more locale-specific members (which are not members of the basic character set) called extended characters. The combined set is also called the extended character set. The values of the members of the execution character set are implementation-defined.

There are several national variants of ASCII. Therefore, the original ASCII is often referred as US-ASCII. The international standard ISO 646 defines a character set similar to US-ASCII, but with code positions corresponding to US-ASCII characters @[]{|} as "national use positions". It also gives some liberties with characters #$^`~. In ISO 646, several "national variants of ASCII" have been defined, assigning different letters and symbols to the "national use" positions. Thus, the characters that appear in those positions - including those in US-ASCII are somewhat "unsafe" in international data transfer. Thus, due to the "national variants," some characters are less "safe" than others--they might be transferred or interpreted incorrectly.

In addition to the letters of the English alphabet ("A" through "Z" and "a" through "z"), the digits ("0" through "9"), and the space, only the following characters can be regarded as really "safe:"

! " % & ' ( ) * + , - . / : ; < = > ?

When naming files, variables, etc., only these characters should be used.

Non-Compliant Coding Example

In the following non-compliant code, unsafe characters are used as part of a filename.

#include <fcntl.h>
#include <sys/stat.h>

int main(void) {
   char *file_name = "&#xBB;&#xA3;???&#xAB;";
   mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;

   int fd = open(file_name, O_CREAT | O_EXCL | O_WRONLY, mode);
   if (fd == -1) {
      /* Handle Error */
   }
}

An implementation is free to define its own mapping of the non-"safe" characters. For example, when tested on a Red Hat Linux distribution, the following filename resulted:

??????

Compliant Solution

Use a descriptive filename, containing only the subset of ASCII described above.

#include <fcntl.h>
#include <sys/stat.h>

int main(void) {
   char *file_name = "name.ext";
   mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;

   int fd = open(file_name, O_CREAT | O_EXCL | O_WRONLY, mode);
   if (fd == -1) {
      /* Handle Error */
   }
}

Risk Assessment

Failing to use only the subset of ASCII guaranteed to work can result in misinterpreted data.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

MSC09-A

1 (low)

1 (unlikely)

3 (low)

P3

L3

Reference

[[Kuhn 06]] UTF-8 and Unicode FAQ for Unix/Linux
[[ISO/IEC 646-1991]] ISO 7-bit coded character set for information interchange
[[ISO/IEC 9899-1999]] Section 5.2.1, "Character sets"
[[MISRA 04]] Rule 3.2, "The character set and the corresponding encoding shall be documented," and Rule 4.1, "Only those escape sequences that are defined in the ISO C standard shall be used"

  • No labels