Rule 10.3 - Bitfield narrower essential type - 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: Rule 10.3 - Bitfield narrower essential type (/showthread.php?tid=1512) |
Rule 10.3 - Bitfield narrower essential type - apereira - 24-10-2019 Hello, Assuming that I have the following structure and enum: Code: typedef struct s{ Why does the following code trigger rule 10.3? Code: s fcn(void) Re: Rule 10.3 - Bitfield narrower essential type - misra-c - 27-11-2019 This response assumes an 8-bit char, 16-bit short and 32-bit int. The essential type for bit-fields is explained in appendix D.4. It is the smallest standard type which is able to represent all possible values of the bit-field. Therefore the essential type for test.a is "unsigned char". The essential type of "(unsigned int)THREE" is "unsigned int" as the cast operation is not listed in Appendix D.7. There is therefore a "narrower conversion" from "unsigned int" to "unsigned char" and hence a violation of rule 10.3. The following would be valid with this rule: Code: test.a = (unsigned char)THREE; |