Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Nontrivial C++ programs are generally divided into multiple translation units that are later linked together to form an executable. To support such a model, C++ restricts named object definitions to ensure that linking will behave deterministically by requiring a single definition for an object across all translation units. This model is called the one-definition rule (ODR), which is defined by the C++ Standard, [basic.def.odr], in paragraph 4 [ISO/IEC 14882-2014], as:

Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly-defined. An inline function shall be defined in every translation unit in which it is odr-used.

The most common approach to multitranslation unit compilation involves declarations residing in a header file that is subsequently made available to a source file via #include. These declarations are often also definitions, such as class and function template definitions. This approach is allowed by an exception defined in paragraph 6, which reads , in part, states the following:

There can be more than one definition of a class type, enumeration type, inline function with external linkage, class template, non-static function template, static data member of a class template, member function of a class template, or template specialization for which some template parameters are not specified in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit....

If the definitions of D satisfy all these requirements, then the program shall behave as if there were a single definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined.

...

The correct mitigation depends on programmer intent. If the programmer intends for the same class definition to be visible in both translation units because of common usage, the solution is to use a header file to introduce the object into both translation units, as shown in this compliant solution:.

Code Block
bgColor#ccccff
langcpp
// S.h
struct S {
  int a;
};
 
// a.cpp
#include "S.h"
 
// b.cpp
#include "S.h"

...

If the ODR violation was a result of accidental name collision, the best mitigation solution is to ensure that both class definitions are unique, as in this compliant solution:.

Code Block
bgColor#ccccff
langcpp
// a.cpp
namespace {
struct S {
  int a;
};
}
 
// b.cpp
namespace {
class S {
public:
  int a;
};
}

...

It is possible for the preceding noncompliant code example to result in a.cpp allocating space for an object with a different size than expected by init_s() in s.cpp. When translating s.cpp, the layout of the structure may include padding bytes between the c and a data members. When translating a.cpp, the layout of the structure may remove those padding bytes as a result of the #pragma pack directive, and so the object passed to init_s() may be smaller than expected. Consequently, when init_s() initializes the data members of s, it may result in a buffer overrun.

...

In this compliant solution, the implementation-defined structure member-alignment directive is removed, ensuring that all definitions of S comply with the ODR:.

Code Block
bgColor#ccccff
langcpp
// s.h
struct S {
  char c;
  int a;
};
 
void init_s(S &s);
 
// s.cpp
#include "s.h"
 
void init_s(S &s); {
  s.c = 'a';
  s.a = 12;
}
 
// a.cpp
#include "s.h"
 
void f() {
  S s;
  init_s(s);
}

...

A compliant solution must change one of three factors: it (1) it must not odr-use n within f(), it (2) it must declare n such that it has external linkage, or (3) it must not use an inline definition of f().

If circumstances allow modification of the signature of g() to accept parameters by value instead of by reference, then n will not be odr-used within f() because n would then qualify as a constant expression. This solution is compliant , but it is not ideal. It may not be possible (or desirable) to modify the signature of g(), such as if g() represented std::max() from <algorithm>. Also, because of the differing linkage used by n and f(), accidental violations of the ODR are still likely if the definition of f() is modified to odr-use n.

...

In this compliant solution, the constant object n is replaced with an enumerator of the same name. Named enumerations defined at namespace scope have the same linkage as the namespace they are contained in. The global namespace has external linkage, and so the definition of the named enumeration , and its contained enumerators , also have external linkage. Although less aesthetically pleasing, this compliant solution does not suffer from the same maintenance burdens of the previous code because n and f() have the same linkage:.

Code Block
bgColor#ccccff
langcpp
enum Constants {
  N = 42
};

int g(const int &lhs, const int &rhs);
 
inline int f(int k) {
  return g(k, N);
}

...

Violating the ODR causes undefined behavior, which can result in exploits as well as denial-of-service attacks. As shown in "Support for Whole-Program Analysis and the Verification of the One-Definition Rule in C++" [Quinlan 06], failing to enforce the ODR enables a virtual function pointer attack known as the VPTR exploit. In this exploit, an object's virtual function table is corrupted so that calling a virtual function on the object results in malicious code being executed. See the paper by Quinlan and colleagues for more details. However, note that to introduce the malicious class, the attacker must have access to the system building the code.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

DCL60-CPP

High

Unlikely

High

P3

L3

Automated Detection

Tool

Version

Checker

Description

Astrée

Include Page
Astrée_V
Astrée_V

type-compatibility
definition-duplicate
undefined-extern
undefined-extern-pure-virtual
external-file-spreading
type-file-spreading
Partially checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC++-DCL60
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

LANG.STRUCT.DEF.FDH
LANG.STRUCT.DEF.ODH

Function defined in header file
Object defined in header file
Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C++1067, C++1509, C++1510


LDRA tool suite
Include Page
LDRA_V
LDRA_V

286 S, 287 S

Fully implemented

PRQA QA-
Parasoft C/C++test

Include Page

PRQA QA-C++_VPRQA QA-C++_V1067
1509 

Parasoft_V
Parasoft_V

CERT_CPP-DCL60-a

A class, union or enum name (including qualification, if any) shall be a unique identifier

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C++: DCL60-CPPChecks for inline constraints not respected (rule partially covered)
RuleChecker
Include Page
RuleChecker_V
RuleChecker_V
type-compatibility
definition-duplicate
undefined-extern
undefined-extern-pure-virtual
external-file-spreading
type-file-spreading
Partially checked

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Bibliography

[ISO/IEC 14882-2014]

Subclause 3.2, "One Definition Rule"

[Quinlan
06
2006]
 

...



...

Image Modified Image Modified Image Modified