Menu

MISRA 10.4 Array Index False Positive?

Amir
2021-02-15
2021-02-16
  • Amir

    Amir - 2021-02-15

    Hello,

    I'm wondering if the following is a false positive:

    bool Interpolate_S16(int16_t *y_out, int16_t x_in, const uint8_t col_size, int16_t *table)
    {
       int16_t y1, y2, x1, x2, dx, dy;
       int16_t *arr_y = &table[0];
       int16_t *arr_x = &table[col_size];
       uint16_t y1_index;
       bool ret_val = false;
    
       if (x_in < arr_x[0])
       {
          *y_out = arr_y[0];
       }
       else if (x_in >= arr_x[col_size - 1])
       ...
       }
    

    Let's ignore the comma's for now. The following line is giving me MISRA 10.4 violation because of col_size. If I change col_size to an int8_t, the violation goes away:

    else if (x_in >= arr_x[col_size - 1])

    I don't see any requirement that the index of an array must be the same type according to MISRA. I believe this is a false positive. unsigned ints should be allowed as array indexes for signed ints, and do not appear to be a violation of 10.4.

    Regards,
    Amir

     
  • Georgiy Komarov

    Georgiy Komarov - 2021-02-16

    Hello, Amir,
    I think this is not a false positive, because 1 in this example have signed integer type, when col_size is unsigned uint8t. I suggest to use 1u instead:

    else if (x_in >= arr_x[col_size - 1u])
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.