Address taken of a standard library function
ID |
c.maintainability.address_of_standard_function |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Undefined Behavior |
Language |
C / C++ |
Description
This code takes the address of a standard library function. The standard permits an implementation to provide these as macros, overload sets, or noexcept-decorated functions, so a conforming standard library is free to reject or misresolve this expression — the standard makes taking their address unspecified behavior. Wrap the call in a lambda instead of forming a pointer directly to the library function.
Rationale
This code takes the address of a standard library function. The standard permits an implementation to provide these as macros, overload sets, or noexcept-decorated functions, so a conforming standard library is free to reject or misresolve this expression — the standard makes taking their address unspecified behavior. Wrap the call in a lambda instead of forming a pointer directly to the library function.
The following code illustrates the pattern detected by this rule:
void f()
{
// FLAGGED: Address taken of a standard library function
auto a = &std::printf;
// FLAGGED: Address taken of a standard library function
auto b = &std::strlen;
// FLAGGED: Address taken of a standard library function
auto c = &std::memcpy;
// FLAGGED: Address taken of a standard library function
auto d = &std::qsort;
// FLAGGED: Address taken of a standard library function
auto e = &std::atoi;
int x = 5;
auto p = &x;
void (*custom)(void) = nullptr;
auto q = custom;
}