I have the following class:
class MyClass {
public:
MyClass( char* what ) : controlled( what ) {}
~MyClass() { delete[] controlled; }
operator char*() const { return controlled; }
operator void*() const { return controlled; }
operator bool() const { return controlled != 0; }
private:
char* controlled;
};
This is compiled with Microsoft SDK that has the following typedefs:
typedef long LONG_PTR;
typedef LONG_PTR LPARAM;
The calling code does the following:
MyClass instance( new char[1000] );
LPARAM castResult = (LPARAM)instance;
// Then we send message intending to pass the address of the buffer inside MyClass
::SendMessage( window, message, wParam, castResult );
Suddenly castResult
is 1
- MyClass::operator bool()
is invoked, it returns true
which is converted to 1
. So instead of passing the address I pass 1
into SendMessage()
which leads to undefined behaviour.
But why is operator bool()
invoked in the first place?