Rule 18.1 and Rule 11.3 compatibility

Moderators: misra-c, david ward

Post Reply
m.bruna
Posts: 2
Joined: Mon Apr 25, 2016 2:51 pm
Company: Siigma Products, s.r.o.

Rule 18.1 and Rule 11.3 compatibility

Post by m.bruna » Fri May 06, 2016 4:29 pm

Hi,

i am not sure about compliance of following example.

Code: Select all

uint32_t varialble = 0;
uint8_t * ptr = (uint8_t *)variable;

(*ptr) = 0;
ptr++;		//line in question
(*ptr) = 0;		//line in question
While exception to the rule 11.3 clearly says, that converting the pointer to object to pointer to char is ok and can be used for accessing individual bytes, Rule 18.1 says that object that is not an array shall be treated as an array of single element and does not allow dereferencing a pointer beyond the end of it.

Can an object that is not an array be treated as an array of 8-bit variables with length of its byte size for this purpose? or can i only access the first byte of the object this way?

thanks,

Michal

Steve Montgomery
Posts: 104
Joined: Fri Sep 17, 2004 1:31 pm
Company: .
Location: Northumberland or Isle of Skye

Re: Rule 18.1 and Rule 11.3 compatibility

Post by Steve Montgomery » Sat May 07, 2016 5:57 pm

Should the second line of your example read:

Code: Select all

uint8_t * ptr = (uint8_t *)&variable;
i.e. you are taking the address of variable? Otherwise, you are dereferencing a null pointer.

m.bruna
Posts: 2
Joined: Mon Apr 25, 2016 2:51 pm
Company: Siigma Products, s.r.o.

Re: Rule 18.1 and Rule 11.3 compatibility

Post by m.bruna » Sat May 07, 2016 7:59 pm

Yes, sorry, the line in the example should have been like you said.

Code: Select all

uint32_t variable = 0;
uint8_t * ptr = (uint8_t *)&variable;

(*ptr) = 0;
ptr++;      //line in question
(*ptr) = 0;      //line in question


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

Re: Rule 18.1 and Rule 11.3 compatibility

Post by misra-c » Fri Jun 24, 2016 2:19 pm

The intention was that such pointer accesses should be be compliant with Rule 18.1, but the working group accept that this is not clear in the documentation.

A future technical clarification will include the following note in the Amplification for rule 18.1.
Note: A pointer to an object type(T) which has been converted to a pointer to a character type (see Exception to rule 11.3) shall be treated as an array of character type with bound equal to sizeof(T).
In the above example "ptr" should be treated as an "array of 4 unsigned char".
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.18 Pointers and arrays”