Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added another NCCE/CS pair

...

Page properties
hiddentrue

I am uncertain whether it would be interesting or not, but another NCCE/CS pair that is specific to Microsoft Visual Studio would be the generic text mappings use by a lot of Win32 APIs (and Windows code in general). The IDE gives you a flag that you can toggle that specifies whether _UNICODE or _MBCS are defined, and this flag can be translation unit specific. Consequently, it's possible (via compiler flags that aren't as in-your-face as code) to introduce two definitions of APIs involving TCHAR members in different translation units:

Code Block
struct S {
  TCHAR Buffer[1024];
};

I hesitate to add this as an NCCE/CS pair because it's so implementation-specific and I think the point is already made with other examples in this rule. However, this is one of those scenarios that can bite Win32 programmers if they're not observant, and the flag is relatively hidden.

Noncompliant Code Example

In this noncompliant code example, the constant object n has internal linkage but is odr-used within f(), which has external linkage. Because f() is declared as an inline function, the definition of f() must be identical in all translation units. However, each translation unit has a unique instance of n, resulting in a violation of the One Definition Rule.

Code Block
bgColor#FFcccc
langcpp
const int n = 42;
 
int g(const int &lhs, const int &rhs);
inline int f(int k) {
  return g(k, n);
}

Compliant Solution

A compliant solution must change one of three factors: do not odr-use n within f(), declare n such that it has external linkage, or do 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. While this results in a compliant solution, it is not ideal. It may not be possible (or desirable) to modify the signature of g(), such as std::max() from <algorithm>. Also, due to the differing linkage used by n and f(), accidental violations of the One Definition Rule are still likely if the definition of f() is modified to odr-use n.

Code Block
bgColor#ccccff
langcpp
const int n = 42;
 
int g(const int &lhs, const int &rhs);
inline int f(int k) {
  return g(k, 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. While 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);
}

Risk Assessment

Violating the ODR results in 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 the attacker must have access to the system building the code to introduce the malicious class.

...