Does "u8 = 6L;" violate rule 10.3

Moderators: misra-c, david ward

Post Reply
misra-c
Posts: 564
Joined: Thu Jan 05, 2006 1:11 pm

Does "u8 = 6L;" violate rule 10.3

Post by misra-c » Fri Jun 17, 2016 2:11 pm

The following question has been submitted to the working group.

Please can you explain which of the following are non-compliant with rule 10.3.

Code: Select all

typedef signed char sint8_t;
typedef unsigned char uint8_t;
sint8_t s8;
uint8_t u8;
s8 = 6L;      /* not compliant - STLR is essentially signed long */
u8 = 6UL;     /* not compliant - as above */

u8 = 6;       /* compliant by exception 1 */
u8 = 6L;      /* compliant or not ? */
Exception 1 says that
a non-negative integer constant expression of essentially signed type may be assigned to an object of essentially unsigned type if its value can be represented in that type
The value of 6L can be represented in an unsigned char type and so this implies that the above assignment is compliant by exception 1. However this is not consistent with u8 = 6UL being non-compliant.
---
Posted by and on behalf of
the MISRA C Working Group

misra-c
Posts: 564
Joined: Thu Jan 05, 2006 1:11 pm

Re: Does "u8 = 6L;" violate rule 10.3

Post by misra-c » Fri Jun 17, 2016 2:12 pm

The intention was that the exception should only apply to expressions with an essential type whose rank is no greater than signed int. In other words:
"u8 = 6L;" is non compliant as the essential type of 6L is signed long.

This will be corrected in a future Technical Corrigendum.
---
Posted by and on behalf of
the MISRA C Working Group

grunwald
Posts: 14
Joined: Wed May 14, 2014 1:02 pm
Company: Axivion GmbH

Re: Does "u8 = 6L;" violate rule 10.3

Post by grunwald » Fri Feb 14, 2020 5:02 pm

This change has the unfortunate effect that:

Code: Select all

uint16_t x = 50000;
now is no longer valid if int is 16 bits.

Post Reply

Return to “8.10 The essential type model”