MISRA Discussion Forums
10.8 Violation understanding details - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.10 The essential type model (https://forum.misra.org.uk/forumdisplay.php?fid=165)
+---- Thread: 10.8 Violation understanding details (/showthread.php?tid=1463)



10.8 Violation understanding details - ashutosh.parate - 17-01-2019

Dear All,

I have read MISRA guidelines and tried to implement in my source code, After running in static code analysis tool it give me 10.8 violation for the below expression saying

" The value of a composite expression shall not be cast to a different essential type category or a wider essential type."

u16 param_val_u16 = 0U;

param_val_u16 = (u16)(brk_itbv_p_brk_hsd_isense.Voltage_Filtered_u32 / 50U);

Can anyone please try to justify what exactly it meant to make it compliant even if I did the intent explicit type casting of u16 to achieve the destination.

Thanks in advance,


Re: 10.8 Violation understanding details - misra-c - 14-02-2019

This response assumes that
* "brk_itbv_p_brk_hsd_isense.Voltage_Filtered_u32" has an unsigned type of 32 bits
* u16 is an unsigned type of 16 bits.

The //essential type// resulting from the "/" operation will be an unsigned type of 32 bits.
Casting an unsigned type of 32 bits to an unsigned type of 16 bits is not a violation of rule 10.8 as it is a cast to the same //essential type category// and is to a narrower type.