K
KMAPSRULE
I am porting an old C++ program to the current GCC platform and I have
the following setup:
//functions like this
void class::set_some_value( const unsigned char & value )
{
if( value > SOME_VALUE_MAX)
{
this->some_value_ = SOME_VALUE_MAX;
}
else
{
this->some_value_ = value;
}
}
The problem is that the MAX values are actually equal to the max limit
for the type (e.g 255 for the above example.) which means the
comparison against SOME_VALUE_MAX is alway false
Even scarier in than the code is in the unit tests:
xClass->set_some_value( 3000 );
ASSERT( xClass->get_some_value() == SOME_VALUE_MAX) ;
obviously this test fails but apparently the previous owner of the
code didn't care..
so is there a way to test for a too large type being passed into a
function?
, or a compiler flag that will warn about this so its easier to find
all the places this is occurring so I can fix them all?
Thanks
the following setup:
//functions like this
void class::set_some_value( const unsigned char & value )
{
if( value > SOME_VALUE_MAX)
{
this->some_value_ = SOME_VALUE_MAX;
}
else
{
this->some_value_ = value;
}
}
The problem is that the MAX values are actually equal to the max limit
for the type (e.g 255 for the above example.) which means the
comparison against SOME_VALUE_MAX is alway false
Even scarier in than the code is in the unit tests:
xClass->set_some_value( 3000 );
ASSERT( xClass->get_some_value() == SOME_VALUE_MAX) ;
obviously this test fails but apparently the previous owner of the
code didn't care..
so is there a way to test for a too large type being passed into a
function?
, or a compiler flag that will warn about this so its easier to find
all the places this is occurring so I can fix them all?
Thanks