This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MISRA 10.3 FALSE positive

Hi,

I'm using TI ARM compiler v5.1.7. And I think that following case not violates MISRA rule 10.3 by casting uint32_t * to s2*. Or am I missing something?

typedef struct _s1{
uint32_t a;
uint32_t b;
}s1;

typedef struct _s2{
uint16_t d;
uint16_t e;
}s2;

void a(s1 *val);

void a(s1 *val)
{
s2 *tmp = (s2*)NULL;

tmp = (s2*)&(val->a);/** violates rule (MISRA-C:2004 10.3/R) The value of a complex expression of integer type shall only be cast to a type of the same signedness that is no wider than the underlying type of the expression  */

tmp->d = 0U;
}

Luděk Hezina

  • I get two MISRA diagnostics for that line.  Here is what it looks like ...

    "file.c", line 20: warning: (MISRA-C:2004 10.3/R) The value of a complex
              expression of integer type shall only be cast to a type of the same
              signedness that is no wider than the underlying type of the
              expression
         tmp = (s2*)&(val->a); // error
                    ^
    
    "file.c", line 20: warning: (MISRA-C:2004 11.4/A) A cast should not be
              performed between a pointer to object type and a different pointer
              to object type
         tmp = (s2*)&(val->a); // error
               ^
    

    I changed the comment on that source line to just "// error" so it will be shorter.  And I build with --verbose_diagnostics to show more detail on the error.  This detail includes showing the source line with a ^ character under the spot where the error starts.

    I think you should be more concerned about MISRA diagnostic 11.4.  That one is correct.  You are taking the address of a uint32_t scalar, and casting it to the address of a s2 structure.

    Thanks and regards,

    -George

  • Yes 11.4 is correct. But 10.3 I don't see. If I write 


    uint32_t *uintptr = NULL;


    uintptr = (uint32_t*)&(val->a);

    the 10.3 is still there.

    Luděk Hezina

  • Thank you for bringing this to our attention.  I can reproduce this error, and 2 more that are similar.  I filed SDSCM00051003 in the SDOWP system to have this investigated.  Feel free to follow it with the SDOWP link below in my signature.

    Thanks and regards,

    -George