codeql-coding-standards
codeql-coding-standards copied to clipboard
`M5-0-8` - `M5-0-9`: Return value of `static_cast` is wrongly considered as a `cvalue`
Affected rules
- M5-0-8
- M5-0-9
Description
Return value of static_cast
seems to be treated as a cvalue
interferring with several MISRA rules:
M-0-8
When upcasting variable using static_cast
and rightaway using the result in another expression, it triggers a M5-0-9 warning (illustrated in example function false_positive
).
It forces the user to create a intermediate variable containing the result of the static_cast
, then using this intermediate variable in the expression (illustrated in example function true_negative
).
M-0-9
When changing variable's signedness using static_cast
and rightaway using the result in another expression, it triggers a M5-0-9 warning (illustrated in example function false_positive
).
It forces the user to create a intermediate variable containing the result of the static_cast
, then using this intermediate variable in the expression (illustrated in example function true_negative
).
Example
M-0-8
void false_positive() {
std::vector<std::uint8_t> v{0};
std::uint32_t u32{0};
v.at(static_cast<std::size_t>(u32)); // Triggers a M5-0-8 warning
}
void true_negative() {
std::vector<std::uint8_t> v{0};
std::size_t st = static_cast<std::size_t>(u32);
v.at(st); // Does not trigger a M5-0-8 warning
}
M-0-9
void false_positive() {
std::vector<std::uint8_t> v{0};
std::int32_t s32{0};
v.at(static_cast<std::size_t>(s32)); // Triggers a M5-0-9 warning
}
void true_negative() {
std::vector<std::uint8_t> v{0};
std::size_t st = static_cast<std::size_t>(s32);
v.at(st); // Does not trigger a M5-0-9 warning
}