MISRA Discussion Forums

Full Version: 11.3 - char * to uint8_t *
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In the following example there is an implicit conversion from (char *) to (uint8_t *):

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?

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.
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.
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.
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.
Thanks for the response. To summarise, I think the following is correct:

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