主要内容

CERT C:Rec.DCL02-C

Use visually distinct identifiers

描述

规则定义

Use visually distinct identifiers.1

Polyspace 实现

规则检查项会检查是否使用排字易混淆的标识符

示例

全部展开

问题

当您在相同命名空间中使用具有重叠可见性的标识符,并且这些标识符在排字上易混淆时,会出现此问题。

风险

“明确易辨”的含义取决于编写源代码时所用的字母表和语言。当您使用在排字上很接近的标识符时,您可能会产生混淆。

对于英语单词中使用的拉丁字母,至少要避免标识符之间只有以下区别:

  • 小写字母与其对应的大写字母互换。

  • 是否存在下划线字符。

  • 字母 O 与数字 0 互换。

  • 字母 I 与数字 1 互换。

  • 字母 I 与字母 l 互换。

  • 字母 S 与数字 5 互换。

  • 字母 Z 与数字 2 互换。

  • 字母 n 与字母 h 互换。

  • 字母 B 与数字 8 互换。

  • 字母 rn 与字母 m 互换。

示例 - 排字易混淆的标识符
void func(void) {
    int id1_numval;
    int id1_num_val;  /* Non-compliant */

    int id2_numval;
    int id2_numVal;   /* Non-compliant */

    int id3_lvalue;
    int id3_Ivalue;   /* Non-compliant */

    int id4_xyZ;
    int id4_xy2;      /* Non-compliant */

    int id5_zerO;
    int id5_zer0;     /* Non-compliant */

    int id6_rn;
    int id6_m;        /* Non-compliant */
}

在此示例中,当使用可能会相互混淆的标识符时,便会违反规则。

检查信息

组:Rec.02.声明和初始化 (DCL)

版本历史记录

在 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.