Search code examples
type-conversionunsignedsignedmisra

Conversion from int16 to unsigned int16 may overflow


After Polyspace code check I am getting "conversion from int16 to unsigned int16 may overflow".

uint16 lData = 0x00u;
sint16 AnalogInputValue;
lData = (uint16)AnalogInputValue; => This line causes Polyspace error

Should the type cast do the job ? According to Polyspace no :)


Solution

  • You first need to assure that AnalogInputValue does not contain any negative numbers before you type cast it as uint16. If you do ot do that then you risk to loose data in the type cast.