11.5: Conversion from point to void

Moderators: misra-c, david ward

Post Reply
michael.metivier
Posts: 10
Joined: Thu Mar 21, 2013 7:30 pm
Company: Gentex, Corp.

11.5: Conversion from point to void

Post by michael.metivier » Tue Jun 17, 2014 3:19 pm

According to the Exception to 11.3, it is permissible to convert a pointer to object into pointer to char, signed char, or unsigned char. There doesn't appear to be an Example within the MISRA document, but I believe it would look like

Code: Select all

uint8_t  *p8;
uint16_t *p16;

p8 = ( uint8_t * ) p16;  /* Compliant - exception */
According to 11.5, it is permissible to convert a pointer to object into a pointer to void, but any conversion of pointer to void into pointer to object is non-compliant:

Code: Select all

uint16_t *p16;
uint32_t *p32;
void     *p;

p = ( void * ) p16;       /* Compliant */
p32 = ( uint32_t * ) p; /* Non-compliant */
According to the C99 specification, "A pointer to void shall have the same representation and alignment requirements as a pointer to character type."
Given that the MISRA rules, themselves, allow conversion of pointer to object into both pointer to void and pointer to character, and that the spec indicates that pointer to void and pointer to character can not have conflicting alignment requirements, should it be allowed to convert from pointer to void to pointer to character:

Code: Select all

void    *p;
uint8_t *p8;

p8 = ( uint8_t * ) p; /* Compliant? */

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

Re: 11.5: Conversion from point to void

Post by misra-c » Wed Jun 25, 2014 6:12 pm

The exception to rule 11.3 exists because conversion from a "pointer to object" to a "pointer to character" type is a commonly used programming idiom for accessing the underlying byte data of the object.

There is no such compelling requirement for conversions from other types such as "pointer to void" and so no exception has been made in that case.

The conversion from void* to another pointer type requires a deviation as no type information is included. This includes conversions from void* to char*.
---
Posted by and on behalf of
the MISRA C Working Group

michael.metivier
Posts: 10
Joined: Thu Mar 21, 2013 7:30 pm
Company: Gentex, Corp.

Re: 11.5: Conversion from point to void

Post by michael.metivier » Thu Jun 26, 2014 12:31 am

For functions which take arbitrary data and operate on it in a byte-wise fashion, for example, calculating a CRC, which would be the preferred method according to the MISRA guidelines:

Code: Select all

uint32_t CRC(size_t length, void * data);
which internally converts data to a pointer to uint8_t (with the requisite deviation), or

Code: Select all

uint32_t CRC(size_t length, uint8_t * data);
and would this require an explicit cast to pointer to uint8_t at each calling location? Or would the Exception to 11.3 allow for an implicit conversion?

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

Re: 11.5: Conversion from point to void

Post by misra-c » Thu Jun 26, 2014 9:47 am

The MISRA Working group can not make a comment on a preferred style of programming.

The exception to 11.3 only refers to casts, since rule 11.3 only applies to casts. Implicit conversions from pointer to object type to pointers to character type are constraint errors( C90 6.3.16.1, C99 6.5.16.1).
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.11 Pointer type conversions”