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

Compare with Current View Page History

« Previous Version 94 Next »

According to the C Standard, 7.1.3 [ISO/IEC 9899:2011],

  • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
  • All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
  • Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included, unless explicitly stated otherwise.
  • All identifiers with external linkage (including future library directions) and errno are always reserved for use as identifiers with external linkage.
  • Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.

Additionally, subclause 7.31 defines many other reserved identifiers for future library directions.

Noncompliant Code Example (Header Guard)

A common, but noncompliant, practice is to choose a reserved name for a macro used in a preprocessor conditional guarding against multiple inclusions of a header file (see also PRE06-C. Enclose header files in an inclusion guard). The name may clash with reserved names defined by the implementation of the C standard library in its headers or with reserved names implicitly predefined by the compiler even when no C standard library header is included.

#ifndef _MY_HEADER_H_
#define _MY_HEADER_H_

/* Contents of <my_header.h> */

#endif /* _MY_HEADER_H_ */

Compliant Solution (Header Guard)

This compliant solution avoids using leading underscores in the name of the header guard:

#ifndef MY_HEADER_H
#define MY_HEADER_H

/* Contents of <my_header.h> */

#endif /* MY_HEADER_H */

Noncompliant Code Example (File Scope Objects)

In this noncompliant code example, the names of the file scope objects _max_limit and _limit both begin with an underscore. Because _max_limit is static, this declaration might seem to be impervious to clashes with names defined by the implementation. However, because the header <stddef.h> is included to define size_t, a potential for a name clash exists. (Note, however, that a conforming compiler may implicitly declare reserved names regardless of whether any C standard library header is explicitly included.) In addition, because _limit has external linkage, it may clash with a symbol with the same name defined in the language runtime library even if such a symbol is not declared in any header. Consequently, it is not safe to start the name of any file scope identifier with an underscore even if its linkage limits its visibility to a single translation unit.

#include <stddef.h>

static const size_t _max_limit = 1024;
size_t _limit = 100;

unsigned int getValue(unsigned int count) {
  return count < _limit ? count : _limit;
}

Compliant Solution (File Scope Objects)

In this compliant solution, names of file scope objects do not begin with an underscore:

#include <stddef.h>

static const size_t max_limit = 1024;
size_t limit = 100;

unsigned int getValue(unsigned int count) {
  return count < limit ? count : limit;
}

Noncompliant Code Example (Reserved Macros)

In this noncompliant code example, because the C standard library header <inttypes.h> is specified to include <stdint.h>, the name SIZE_MAX conflicts with a standard macro of the same name, used to denote the upper limit of size_t. In addition, although the name INTFAST16_LIMIT_MAX is not defined by the C standard library, it is a reserved identifier because it begins with the INT prefix and ends with the _MAX suffix (see the C Standard, 7.31.10).

#include <inttypes.h>
#include <stdio.h>

static const int_fast16_t INTFAST16_LIMIT_MAX = 12000;

void print_fast16(int_fast16_t val) {
  enum { SIZE_MAX = 80 };
  char buf[SIZE_MAX];
  if (INTFAST16_LIMIT_MAX < val) {
    sprintf(buf, "The value is too large");
  } else {
    snprintf(buf, SIZE_MAX, "The value is %" PRIdFAST16, val);
  }
}

Compliant Solution (Reserved Macros)

This compliant solution avoids redefining reserved names or using reserved prefixes and suffixes:

#include <inttypes.h>
#include <stdio.h>
 
static const int_fast16_t MY_INTFAST16_UPPER_LIMIT = 12000;

void print_fast16(int_fast16_t val) {
  enum { BUFSIZE = 80 };
  char buf[BUFSIZE];
  if (MY_INTFAST16_UPPER_LIMIT < val) {
    sprintf(buf, "The value is too large");
  } else {
    snprintf(buf, BUFSIZE, "The value is %" PRIdFAST16, val);
  }
}

Noncompliant Code Example (Identifiers with External Linkage)

In addition to symbols defined as functions in each C standard library header, identifiers with external linkage include errno and math_errhandling, among others, regardless of whether any of them are masked by a macro of the same name.

This noncompliant example provides definitions for the C standard library functions malloc() and free(). Although this practice is permitted by many traditional implementations of UNIX (for example, the Dmalloc library), it is undefined behavior according to the C Standard. Even on systems that allow replacing malloc(), doing so without also replacing aligned_alloc(), calloc(), and realloc() is likely to cause problems.

#include <stddef.h>
 
void *malloc(size_t nbytes) {
  void *ptr;
  /* Allocate storage from own pool and set ptr */
  return ptr;
}

void free(void *ptr) {
  /* Return storage to own pool */
}

Compliant Solution (Identifiers with External Linkage)

The compliant, portable solution avoids redefining any C standard library identifiers with external linkage. In addition, it provides definitions for all memory allocation functions:

#include <stddef.h>

void *my_malloc(size_t nbytes) {
  void *ptr;
  /* Allocate storage from own pool and set ptr */
  return ptr;
}

void *my_aligned_alloc(size_t alignment, size_t size) {
  void *ptr;
  /* Allocate storage from own pool, align properly, set ptr */
  return ptr;
}

void *my_calloc(size_t nelems, size_t elsize) {
  void *ptr;
  /* Allocate storage from own pool, zero memory, and set ptr */
  return ptr;
}

void *my_realloc(void *ptr, size_t nbytes) {
  /* Reallocate storage from own pool and set ptr */
  return ptr;
}

void my_free(void *ptr) {
  /* Return storage to own pool */
}

Noncompliant Code Example (errno)

According to the C Standard, 7.5, paragraph 2 [ISO/IEC 9899:2011], the behavior of a program is undefined when

a macro definition of errno is suppressed in order to access an actual object, or the program defines an identifier with the name errno

See undefined behavior 114.

The errno identifier expands to a modifiable lvalue that has type int but is not necessarily the identifier of an object. It might expand to a modifiable lvalue resulting from a function call, such as *errno(). It is unspecified whether errno is a macro or an identifier declared with external linkage. If a macro definition is suppressed to access an actual object, or if a program defines an identifier with the name errno, the behavior is undefined.

Legacy code is apt to include an incorrect declaration, such as the following:

extern int errno;

Compliant Solution (errno)

The correct way to declare errno is to include the header <errno.h>:

#include <errno.h>

Implementations conforming to C are required to declare errno in <errno.h>, although some historic implementations failed to do so.

Exceptions

DCL37-C-EX1: Provided that a library function can be declared without reference to any type defined in a header, it is permissible to declare that function without including its header provided that declaration is compatible with the standard declaration.

/* Not including stdlib.h */
void free(void *);
 
void func(void *ptr) {
  free(ptr);
}

Such code is compliant because the declaration matches what stdlib.h would provide and does not redefine the reserved identifier. However, it would not be acceptable to provide a definition for the free() function in this example.

DCL37-C-EX2: For compatibility with other compiler vendors or language standard modes, it is acceptable to create a macro identifier the same as a reserved identifier so long as the behavior is idempotent, as in this example:

/* Sometimes generated by configuration tools such as autoconf */
#define const const
 
/* Allowed compilers with semantically equivalent extension behavior */
#define inline __inline

DCL37-C-EX3: As a compiler vendor or standard library developer, it is acceptable to use identifiers reserved for your implementation. Reserved identifiers may be defined by the compiler, in standard library headers, or headers included by a standard library header, as in this example declaration from the glibc standard C library implementation:

/*
  The following declarations of reserved identifiers exist in the glibc implementation of
  <stdio.h>. The original source code may be found at:
  https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=include/stdio.h;hb=HEAD
*/
 
#  define __need_size_t
#  include <stddef.h>
/* Generate a unique file name (and possibly open it).  */
extern int __path_search (char *__tmpl, size_t __tmpl_len,
			  const char *__dir, const char *__pfx,
			  int __try_tempdir);

Risk Assessment

Using reserved identifiers can lead to incorrect program operation.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

DCL37-C

Low

Unlikely

Low

P3

L3

Automated Detection

Tool

Version

Checker

Description

Compass/ROSE

 

 

 

ECLAIR1.2CC2.DCL37Fully implemented

LDRA tool suite

9.7.1

86 S, 218 S, 219 S, 580 S, 626 S

Fully Implemented

Parasoft C/C++test9.5MISRA2004-20_1_aFully implemented
PRQA QA-C
Unable to render {include} The included page could not be found.
0602, 4600, 4601, 4602,
4603, 4604, 4605, 4606,
4607, 4608
 

Related Guidelines

Bibliography

[IEEE Std 1003.1-2013]Section 2.2, "The Compilation Environment"
[ISO/IEC 9899:2011]7.1.3, "Reserved Identifiers"
7.31.10, "Integer Types <stdint.h>"

 


  • No labels