overflow in function parameter

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
 
S

sebastian

sure, just change the parameter to the largest integral type your
system supports, ie:

void class::set_some_value( const unsigned long & value );
 
K

KMAPSRULE

sure, just change the parameter to the largest integral type your
system supports, ie:

void class::set_some_value( const unsigned long & value );

i'd like to do that,
but reuse rules wont allow a signature change.

I figured that was probably the only real solution but figured I would
Ask,
an overtired mind is a dangerous thing!.


Thank you for your reply :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top