...
An unnamed-namespace-definition behaves as if it were replaced by:
inline namespace unique { /* empty body */ }
using namespace unique ;
namespace unique { namespace-body }where
inline
appears if and only if it appears in the unnamed-namespace-definition, all occurrences ofunique
in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the entire program.
Page properties | ||
---|---|---|
| ||
ODR-use should be further clarified. |
Production-quality C++ code frequently use uses header files as a means to share code between translation units. A header file is any file that is inserted into a translation unit through a #include
directive. Do not define an unnamed namespace in a header file. When an unnamed namespace is defined within in a header file, it can lead to surprising results. Due to default internal linkage, each translation unit will define its own unique instance of members of the unnamed namespace that are ODR-used within that translation unit. This can cause unexpected results, bloat the resulting executable, or inadvertently trigger undefined behavior due to One Definition Rule (ODR) violations.
...
In this noncompliant code example, the variable v
is defined in an unnamed namespace within a header file, and an inline function, get_v()
, is defined, which accesses that variable. ODR-using the inline function from multiple translation units (as shown in the implementation of f()
and g()
) results in a violation of the One Definition Rule because the definition of get_v()
is not identical in all translation units (due to referencing a unique v
in each translation unit).
...
In this compliant solution, v
is defined in only one translation unit , but is externally visible to all translation units and can be accessed from the inline get_v()
function:
...
In this noncompliant code example, the function f()
is defined within a header file. However, including the header file in multiple translation units results in causes a violation of the One Definition Rule that usually results in an error diagnostic generated at link time due to multiple definitions of a function with the same name.
...
This noncompliant code example attempts to resolve the link-time errors by defining f()
within an unnamed namespace. However, this results in it produces multiple, unique definitions of f()
in the resulting executable. If a.h
is included from many translation units, this it can lead to increased link times, a larger executable file, and reduced performance.
...
In this compliant solution, f()
is not defined with an unnamed namespace , and is instead defined as an inline function. Inline functions are required to be defined identically in all translation units in which they are used, which allows an implementation to generate only a single instance of the function at runtime in the event the body of the function does not get generated for each call site.
...
In this noncompliant code example, the variable v
is defined in an unnamed namespace within a header file , and is accessed from two separate translation units. Each translation unit prints the current value of v
and then assigns a new value into it. However, because v
is defined within an unnamed namespace, each translation unit operates on its own instance of v
, resulting in unexpected output.
...
When executed, this program will printprints the following:
Code Block |
---|
f(): 0 g(): 0 f(): 42 g(): 100 |
...
In this compliant solution, v
is defined in only one translation unit , but is externally visible to all translation units, resulting in the expected behavior:
...
When executed, this program will printprints the following:
Code Block |
---|
f(): 0 g(): 42 f(): 100 g(): 42 |
Page properties | ||
---|---|---|
| ||
I suspect that we may want an exception for allowing unnamed namespaces in header files under certain cirumstancescircumstances. For instance, it may be permissible for constant values in unnamed namespaces (but then again, those could still result in ODR violations). I know that some implementations of |
...
Defining an unnamed namespace within a header file can cause data integrity violations and performance problems , but is unlikely to go unnoticed with sufficient testing. One Definition Rule violations result in undefined behavior.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL59-CPP | Medium | Unlikely | Medium | P4 | L3 |
...
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...