MISRA Discussion Forums
11.3 - char * to uint8_t * - 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.3 - char * to uint8_t * (/showthread.php?tid=1238)



11.3 - char * to uint8_t * - delta_controls - 27-04-2016

In the following example there is an implicit conversion from (char *) to (uint8_t *):

Code:
extern void print(const uint8_t *text);

int main(void)
{
    print("Some text");
}

Am I correct in thinking that this is compliant? Or does the exception to rule 11.3 only apply when conversions are explicit?

Thanks.


Re: 11.3 - char * to uint8_t * - delta_controls - 05-05-2016

Having given this more thought, I have realised that the example is not relevant to the rule, as casts are explicit conversions by definition.

My understanding is that there are no rules with which the example is non-compliant and the conversion is safe.


Re: 11.3 - char * to uint8_t * - delta_controls - 10-05-2016

This is due to a misinterpretation of Directive 4.6, which doesn't apply to data that are essentially character. In the example, plain char should have been used for the input parameter.


Re: 11.3 - char * to uint8_t * - delta_controls - 10-05-2016

The conversion in the example is actually a constraint violation and an explicit cast would be required. Rule 11.3 would then be satisfied by exception.


Re: 11.3 - char * to uint8_t * - misra-c - 20-05-2016

This response assumes that uint8_t is defined as "unsigned char".

Section 8.11 of the MISRA-C:2012 guidelines summarizes the implicit conversions that are permitted by the C language. The list of permitted implicit conversions does not include conversions from char* to unsigned char*. Therefore your example violates the constraints of the C language and hence is not compliant with rule 1.1 of the MISRA-C:2012 guidelines.

The example is also not compliant with rule 7.4, which states that "A string literal shall not be assigned to an object unless the object's type is "pointer to const-qualified char"". Assigned includes the implicit conversion that occur on passing an argument to a function.


Re: 11.3 - char * to uint8_t * - delta_controls - 24-05-2016

Thanks for the response. To summarise, I think the following is correct:

Code:
extern void print1(const char *text);
extern void print2(const uint8_t *text);

int main(void)
{
    const char *txt = "Some text";
    
    print1("Some text");                // Compliant (preferred solution)
    print2((uint8_t *)"Some text");     // Compliant
    print2((uint8_t *)txt);             // Compliant
}