In the following code I am seeing what I think are false positives for the CodeCheck rule MISRA23_8.2.6:
#include
<cstdint>
#include
<utility>
int
main() {
uint16_t
test_value
=
0x1234;
auto*
ptr
= reinterpret_cast<uint8_t*>(&test_value);
std::swap(ptr[0],
ptr[1]);
return
0;
}
MISRA23_8.2.5: Line 5: reinterpret_cast is used
MISRA23_8.2.6: Line 5: Integer converted to pointer.
For MISRA23_8.2.6, the rule states:
An object with integral, enumerated, or pointer to void type shall not be cast to a pointer type
In this particular instance, it is not an integral type that is being cast to a pointer type, but a pointer to an integral type that is being cast to a different pointer type. From the description of this rule and the examples provided I think this may be a false positive, but I admit that I am not 100% on the interpretation of this rule.
I have also left in the violation for MISRA23_8.2.5 here. One of the exceptions to this rule is:
Using reinterpret_cast< T * > to cast any object pointer to a pointer to T, where T is one of void, char, unsigned char or std::byte, possibly cv-qualified
It is my understanding that unsigned char and uint8_t are effectively the same type. Spefically, typeid(uint8_t).name() and typeid(unsigned char).name() return the same thing (at least on my system, I am assuming this holds for other systems too). Is it reasonable to extend this exemption to include uint8_t? The same argument does not seem to apply for int8_t and char though.
The main reason I bring this up is because MISRA23_6.9.2 says
The names of the standard signed integer types and standard unsigned integer types should not be used
Specifically, it says that
This rule applies to the names of integral types constructed using the keywords char, short, int, long, signed and unsigned (ignoring any cv-qualification). It does not apply to the use of plain char
And so, based on this rule, uint8_t is preferable to unsigned char.
In the following code I am seeing what I think are false positives for the CodeCheck rule MISRA23_8.2.6:
MISRA23_8.2.5: Line 5: reinterpret_cast is used
MISRA23_8.2.6: Line 5: Integer converted to pointer.
For MISRA23_8.2.6, the rule states:
In this particular instance, it is not an integral type that is being cast to a pointer type, but a pointer to an integral type that is being cast to a different pointer type. From the description of this rule and the examples provided I think this may be a false positive, but I admit that I am not 100% on the interpretation of this rule.
I have also left in the violation for MISRA23_8.2.5 here. One of the exceptions to this rule is:
It is my understanding that unsigned char and uint8_t are effectively the same type. Spefically, typeid(uint8_t).name() and typeid(unsigned char).name() return the same thing (at least on my system, I am assuming this holds for other systems too). Is it reasonable to extend this exemption to include uint8_t? The same argument does not seem to apply for int8_t and char though.
The main reason I bring this up is because MISRA23_6.9.2 says
Specifically, it says that
And so, based on this rule, uint8_t is preferable to unsigned char.