Main Content

CERT C: Rec. MSC09-C

R2026b

Character encoding: Use subset of ASCII for safety

Since R2026b

Description

Character encoding: Use subset of ASCII for safety1

Polyspace Implementation

Polyspace® checks for the issue Unsafe characters in filename.

Examples

expand all

Issue

The issue occurs when a filename passed to a file-opening function, such as open, fopen, or freopen, contains characters outside the ASCII subset [0-9a-zA-Z%&+,-.:=_].

Risk

Filenames with non-ASCII or special characters can cause portability issues across operating systems and file systems. Characters such as spaces and backslashes, or extended encoding sequences, can be misinterpreted, leading to unintended file access or file-not-found errors.

Fix
  • Restrict filenames to the safe ASCII subset: digits, uppercase and lowercase letters, and the characters %, &, +, ,, -, ., :, =, and _.

  • Sanitize user input intended to be used as filenames so that it uses only the safe ASCII subset.

Example — Non-ASCII Characters in Filename

The file name "\xe5ngstr\xf6m" contains non-ASCII characters (byte values outside the printable ASCII range). Polyspace reports a violation of this rule on the call to open().


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

int main(void) {
    char *file_name = "\xe5ngstr\xf6m";
    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
    int fd = open(file_name, O_CREAT | O_EXCL | O_WRONLY, mode); // Noncompliant
    if (fd == -1) { }
    return 0;
}
Correction — Use Safe ASCII Filename

To fix this violation, use a filename that contains only these characters: [0-9a-zA-Z%&+,-.:=_].


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

int main(void) {
    char *file_name = "angstrom.dat";
    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
    int fd = open(file_name, O_CREAT | O_EXCL | O_WRONLY, mode); // Compliant
    if (fd == -1) { }
    return 0;
}

Check Information

Group: Rec. 48. Miscellaneous (MSC)
PQL Name: std.cert.MSC09_C

Version History

Introduced in R2026b


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.