...
Non-Compliant Code Example
The first declaration of the identifier x is externally linked, while the second declaration is internally linkedIn this non-compliant code example, i2 and i5 is defined as having both internal and external linkage. Future use of this either identifier results in undefined behavior.
| Code Block | ||
|---|---|---|
| ||
int i1 = x;10; /* definition, externallyexternal linkedlinkage */ static int i2 = 20; /* definition, internal linkage */ int main(void) { extern int i3 = 30; /* definition, external linkage */ int i4; /* tentative definition, external linkage */ static int xi5; /* tentative definition, internal linkage * unlinked/ int i1; /* valid tentative definition */ int i2; /* not legal, linkage disagreement with previous * use of identifier x results in undefined behavior */ }/ int i3; /* valid tentative definition */ int i4; /* valid tentative definition */ int i5; /* not legal, linkage disagreement with previous */ |
Compliant Solution
This compliant solution uses different (and more descriptive) identifiers to avoid any conflictsdoes not include conflicting definitions.
| Code Block | ||
|---|---|---|
| ||
int external_x; /* externally linked i1 = 10; /* definition, external linkage */ static int i2 = 20; /* definition, internal linkage */ extern int main(void) { static int internal_x; /* unlinked */ /* we're good to go */ } i3 = 30; /* definition, external linkage */ int i4; /* tentative definition, external linkage */ static int i5; /* tentative definition, internal linkage */ |
Risk Assessment
Use of an identifier classified as both internally and externally linked causes undefined behavior. However, it would be highly is unlikely that an attacker could exploit this behavior to run arbitrary code.
...