CertC-MSC04

Use comments consistently and in a readable fashion

Required inputs: IR

Noncompliant Code Example

Do not use the character sequence /* within a comment:

/* Comment with end comment marker unintentionally omitted
security_critical_function();
/* Some other comment */

In this example, the call to the security-critical function is not executed. A reviewer examining this page could incorrectly assume that the code is executed.

If execution failure is the result of an accidental omission, it is useful to use an editor that provides syntax highlighting or formats the code to help identify issues like missing end-comment delimiters.

Because missing end delimiters are error prone and often viewed as a mistake, this approach is not recommended for commenting out code.

Compliant Solution (Preprocessor)

Instead of using /* and */ to comment out blocks of code, use conditional compilation (for example, #if , #ifdef , or #ifndef ):

#if 0  /*
        * Use of critical security function no
        * longer necessary.
        */
security_critical_function();
/* Some other comment */
#endif

The text inside a block of code commented out using #if , #ifdef , or #ifndef must still consist of valid preprocessing tokens . This means that the characters " and ' must each be paired just as in real C code, and the pairs must not cross line boundaries. In particular, an apostrophe within a contracted word looks like the beginning of a character constant. Consequently, natural-language comments and pseudocode should always be written between the comment delimiters /* and */ or following // .

Compliant Solution (Compiler)

This compliant solution takes advantage of the compiler's ability to remove unreachable (dead) code. The code inside the if block must remain acceptable to the compiler. If other parts of the program, such as macros, types, or function prototypes, later change in a way that would cause syntax errors, the unexecuted code must be brought up to date to correct the problem. Then, if it is needed again in the future, the programmer need only remove the surrounding if statement and the NOTREACHED comment.

The NOTREACHED comment tells some compilers and static analysis tools not to complain about this unreachable code. It also serves as documentation.

if (0) {  /*
           * Use of critical security function no
           * longer necessary, for now.
           */
  /*NOTREACHED*/
  security_critical_function();
  /* Some other comment */
}

This code is an instance of exception MSC07-C-EX2 to MSC07-C. Detect and remove dead code .

Noncompliant Code Example

Following are some additional examples of comment styles that are confusing and should be avoided:

// */          /* Comment, not syntax error */

f = g/**//h;   /* Equivalent to f = g / h; */

//\
i();           /* Part of a two-line comment */

/\
/ j();         /* Part of a two-line comment */


/*//*/ l();    /* Equivalent to l(); */

m = n//**/o
+ p;           /* Equivalent to m = n + p; */

a = b //*divisor:*/c
+d;            /*
                * Interpreted as a = b/c + d; in c90
                * compiler and a = b + d; in c99 compiler.
                */
Compliant Solution

Use a consistent style of commenting:

/* Nice simple comment */

int i; /* Counter */
Risk Assessment

Confusion over which instructions are executed and which are not can lead to serious programming errors and vulnerabilities, including denial of service , abnormal program termination , and data integrity violation. This problem is mitigated by the use of interactive development environments (IDEs) and editors that use fonts, colors, or other mechanisms to differentiate between comments and code. However, the problem can still manifest, for example, when reviewing source code printed on a black-and-white printer.

Recommendation Severity Likelihood Detectable Repairable Priority Level
MSC04-C Medium Unlikely Yes No P4 L3
Related Guidelines
SEI CERT C++ Coding Standard VOID MSC04-CPP. Use comments consistently and in a readable fashion
MISRA C:2012 Rule 1.2 (advisory)
Rule 3.1 (required)
Directive 4.4 (advisory)
Bibliography
[ Summit 2005 ] Question 11.19
Excerpt from SEI CERT C Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition) and SEI CERT C Coding Standard [https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/miscellaneous-msc/msc04-c], Copyright (C) 1995-2026 Carnegie Mellon University. See 9.4 3rd-Party Licenses in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

nested_c_comment

C-style comment containing /* sequence.

None

False

nested_cpp_comment

Comment containing // sequence.

None

False

Options

allow_urls_in_cpp_comment

allow_urls_in_cpp_comment : bool = True

If enabled, URLs are allowed within /* */ comments despite containing //.

Note: to allow all uses of // within /* */ comments, you can disable the nested_cpp_comment message of this rule.

 

check_cpp_comments

check_cpp_comments : bool = True

Whether to look for /* in C++-style comments as well.
 

files_to_check

files_to_check : bauhaus.rules.axivion.lexical.comments.FileToCheckSet = {'Primary_File', 'User_Include_File'}

Files to apply this check to (Primary_File, User_Include_File, and/or System_Include_File).