MISRA Discussion Forums
11.5: Conversion from point to void - 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.11 Pointer type conversions (https://forum.misra.org.uk/forumdisplay.php?fid=166)
+---- Thread: 11.5: Conversion from point to void (/showthread.php?tid=1068)



11.5: Conversion from point to void - michael.metivier - 17-06-2014

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:
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:
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:
void    *p;
uint8_t *p8;

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



Re: 11.5: Conversion from point to void - misra-c - 25-06-2014

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*.


Re: 11.5: Conversion from point to void - michael.metivier - 25-06-2014

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:
uint32_t CRC(size_t length, void * data);
which internally converts data to a pointer to uint8_t (with the requisite deviation), or

Code:
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?


Re: 11.5: Conversion from point to void - misra-c - 26-06-2014

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).