Main Content

CERT C: Rule PRE30-C

Do not create a universal character name through concatenation

Description

Rule Definition

Do not create a universal character name through concatenation.1

Polyspace Implementation

The rule checker checks for Universal character name from token concatenation.

Examples

expand all

Issue

Universal character name from token concatenation occurs when two preprocessing tokens joined with a ## operator create a universal character name. A universal character name begins with \u or \U followed by hexadecimal digits. It represents a character not found in the basic character set.

For instance, you form the character \u0401 by joining two tokens:

#define assign(uc1, uc2, val) uc1##uc2 = val
...
assign(\u04, 01, 4);

Risk

The C11 Standard (Sec. 5.1.1.2) states that if a universal character name is formed by token concatenation, the behavior is undefined.

Fix

Use the universal character name directly instead of producing it through token concatenation.

Example - Universal Character Name from Token Concatenation
#define assign(uc1, uc2, val) uc1##uc2 = val

int func(void) {
    int \u0401 = 0;
    assign(\u04, 01, 4);  //Noncompliant
    return \u0401;
}

In this example, the assign macro, when expanded, joins the two tokens \u04 and 01 to form the universal character name \u0401.

Correction — Use Universal Character Name Directly

One possible correction is to use the universal character name \u0401 directly. The correction redefines the assign macro so that it does not join tokens.

#define assign(ucn, val) ucn = val

int func(void) {
    int \u0401 = 0;
    assign(\u0401, 4); 
    return \u0401;
}

Check Information

Group: Rule 01. Preprocessor (PRE)

Version History

Introduced in R2019a


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.