Opened 11 years ago
Last modified 9 years ago
#7366 new defect
False positive knownConditionTrueFalse - comparing same address with different casts
| Reported by: | amai | Owned by: | noone |
|---|---|---|---|
| Priority: | Normal | Milestone: | |
| Component: | False positive | Version: | |
| Keywords: | knownConditionTrueFalse | Cc: |
Description (last modified by )
In the following example the reported [a.cpp:2]: (style) Condition '(void*)(TestObject*)4096==(void*)(!TestObjectBaseB*)(TestObject*)4096' is always true is IMHO a false positive.
void foo() { if ((void*)(Object*)0x1000 == (void*)(ObjectBaseB*)(Object*)0x1000) // FP ; } void bar(char* ptr) { if ((void*)(Object*)ptr == (void*)(ObjectBaseB*)(Object*)ptr) // given the current behaviour a "false FN"?! ; }
It seems the 2nd cast is removed internally. OTOH I wonder why there is no warning issued for bar() then?
Debug output:
##file tickets/7366.cpp
1: void foo ( ) {
2: if ( ( Object * ) 4096 == ( Object * ) 4096 ) {
3: ; }
4: }
5: void bar ( char * ptr@1 ) {
6: if ( ptr@1 == ptr@1 ) {
7: ; }
8: }
Change History (8)
comment:1 by , 11 years ago
comment:2 by , 11 years ago
Source is from VirtualBox.
I assume at an embedd platform one might face situations like this.
follow-up: 5 comment:4 by , 10 years ago
I don't see why it's a FP right now. Same address in lhs and rhs is compared, isn't it?
comment:5 by , 10 years ago
Replying to danielmarjamaki:
I don't see why it's a FP right now. Same address in lhs and rhs is compared, isn't it?
I think there's a catch in it...
Have a look at this example:
#include <iostream> using namespace std; class ObjectBaseA { public: virtual ~ObjectBaseA(){} int abasemember; }; class ObjectBaseB { public: virtual ~ObjectBaseB(){} int bbasemember; }; class Object : public ObjectBaseA, public ObjectBaseB { public: virtual ~Object(){} int derivedmember; }; int main() { void* ptr=(void*)0x42; std::cout << (void*)ptr << std::endl; std::cout << (void*)(ObjectBaseB*)ptr << std::endl; std::cout << (void*)(Object*)ptr << std::endl; std::cout << (void*)(ObjectBaseB*)(Object*)ptr << std::endl; return 0; }
comment:6 by , 10 years ago
And some sample output from cygwin 32bit:
$ ./7366.exe 0x42 0x42 0x42 0x4a
comment:7 by , 10 years ago
| Summary: | Questionable knownConditionTrueFalse - comparing casted pointers → False positive knownConditionTrueFalse - comparing same address with different casts |
|---|
comment:8 by , 9 years ago
| Description: | modified (diff) |
|---|
Where does this code come from?
That is normally undefined behaviour.
It is undefined behaviour to create a out-of-bounds pointer. Unless you know that there is an object at address 0x1000 it is UB to point at address 0x1000.
In normal unix/windows platforms you don't know where objects are so I have thought about adding a platform setting that would enable UB warnings when constant pointer values other than 0 are used.