How do explicit casts of arguments affect 21.15 (amendment 1)?

Moderators: misra-c, david ward

Post Reply
abgs
Posts: 12
Joined: Wed Sep 06, 2017 4:25 pm
Company: Gimpel Software

How do explicit casts of arguments affect 21.15 (amendment 1)?

Post by abgs » Mon Apr 09, 2018 8:45 pm

When rule 21.15 of MISRA C 2012 Amendment 1 refers to the types that pointer arguments are pointers to, how are explicit casts interpreted? Are the types that they are pointers to considered before or after the cast?

Code: Select all

void f(signed char* a, int* b, size_t n) {
    memcmp(a, a, n); // ok - pointers to same type
    memcmp(b, b, n); // ok - pointers to same type
    memcmp((int*)b, (int*)b, n); // ok - same type and cast has no effect
    memcmp((int*)a, (int*)a, n); // ok? - cast to different pointer type, arguments match before and after but with different matching types
    memcmp(a, b, n); // violation - pointers to different types
    memcmp((int*)a, b, n); // violation? - types only match due to pointer cast
    memcmp((float*)a, (float*)b, n); // violation? - different pointer types cast to third common pointer type
    memcmp((short*)a, a, n); // violation? - original pointer types match before cast
}

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

Re: How do explicit casts of arguments affect 21.15 (amendment 1)?

Post by misra-c » Tue Apr 24, 2018 10:41 am

Rule 21.15 applies to the types after casting.

Code: Select all

void f(signed char* a, int* b, size_t n) {
    memcmp(a, a, n);                   // Compliant
    memcmp(b, b, n);                   // Compliant
    memcmp((int*)b, (int*)b, n);       // Compliant
    memcmp((int*)a, (int*)a, n);       // Compliant - but violates Rule 11.3
    memcmp(a, b, n);                   // Non-Compliant
    memcmp((int*)a, b, n);             // Compliant - but violates Rule 11.3
    memcmp((float*)a, (float*)b, n);   // Compliant - but violates Rules 11.3 and  21.16
    memcmp((short*)a, a, n);           // Non-Compliant - and also violates Rule 11.3
}
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.21 Standard libraries”