The Optimizer of the cl2000 compiler seams to optimize out local variables, but still accesses them on stack leading to stack corruption and faults on return.
- cl2000 compiler Versions 6.4.2, 6.2.7, 6.2.10
- optimize for space (-ms)
Sample Code:
uint64_t DeviceSearch::find( uint16_t device_code )
{
if ( !check_for_device_presence() )
return NoDevices;
m_master.write_byte( Command::SEARCH_ROM );
if ( !check_for_device_of_family( device_code ) )
return NoMatchingDevice;
uint64_t address = read_device_address( device_code );
if ( address_is_error( address ) )
return address;
if ( !address_crc_valid( address ) )
return AddressCrcError;
return address;
}
uint64_t DeviceSearch::read_device_address( uint16_t device_code )
{
uint64_t address = device_code;
for ( int i=8; i<64; i++ )
{
Master::AddressBits ab = m_master.one_wire_triplet( 0 );
if ( ab == Master::AB_ONE )
address |= (1ull)<<i;
if ( ab == Master::AB_NONE )
return NoMatchingDevice;
if ( ab == Master::AB_BOTH )
return MultipleMatchingDevices;
}
return address;
}
- works if complied without optimization
- if compiled with -ms the "address" vairable in booth functions seams to be optimized away
- stepping in the debugger shows nonsensical address values
- return from find ends up at the wrong address
- making address volatile in read_device_address to prevent optimization off the variable seams to be a work around
- please contact me via email for a sample project which triggers the problem.