Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array subscript is above array bounds [-Warray-bounds]

Tags:

gcc

I looked around and didn't find anything that could help me.... I am building a kernel and am getting a few, array subscript is above array bounds, warnings which FAILS the build. I am using Linaro toolchain and set to -O3 so any warning will FAIL the build... Thanks for all the help

    drivers/media/video/msm/rawchip/Yushan_API.c: In function 'Yushan_Check_Pad_For_IntrID':
drivers/media/video/msm/rawchip/Yushan_API.c:1751:85: warning: array subscript is above array bounds [-Warray-bounds]
error, forbidden warning: Yushan_API.c:1751

Here the method in which it fails...

bool_t  Yushan_Check_Pad_For_IntrID(uint8_t bInterruptId)
{

    uint8_t     bFirstIndexForSet[] = {1, 5, 11, 17, 23, 27, 58, 62, 69, 77, 79, 82, 83};
    uint8_t     bIntrSetID = 0;
    uint16_t    uwIntrSetsDivertedToPad1 = 0;

    VERBOSELOG("[CAM] %s: Start\n", __func__);
    /* Read the list of the interrupt sets diverted to Pad1 */
    SPI_Read(YUSHAN_IOR_NVM_SEND_ITR_PAD1 , 2, (uint8_t *)&uwIntrSetsDivertedToPad1);

    /* Trace through InterruptSets */
    while(bIntrSetID < TOTAL_INTERRUPT_SETS)
    {

        if( (bInterruptId>=bFirstIndexForSet[bIntrSetID])&&(bInterruptId<bFirstIndexForSet[bIntrSetID+1]) )
        {
            if((uwIntrSetsDivertedToPad1>>bIntrSetID)&0x01) {
                VERBOSELOG("[CAM] %s: End\n", __func__);
                return INTERRUPT_PAD_1;
            } else {
                VERBOSELOG("[CAM] %s: End\n", __func__);
                return INTERRUPT_PAD_0;
            }
        } else
            bIntrSetID++;

    }

    /* Just to remove warning */
    VERBOSELOG("[CAM] %s: End\n", __func__);
    return INTERRUPT_PAD_0;

}

It errors on line:

if( (bInterruptId>=bFirstIndexForSet[bIntrSetID])&&(bInterruptId < bFirstIndexForSet[bIntrSetID+1]) )
like image 892
user1952119 Avatar asked Sep 03 '25 17:09

user1952119


1 Answers

The index is exceeding the array's size at this call:

bFirstIndexForSet[bIntrSetID+1]

bFirstIndexForSet has 13 elements, which requires indexes from 0 to 12.

You are looping for every indexes lower than TOTAL_INTERRUPT_SETS, which should exceed 12. (Since 11+1 would be the maximum index your array can use.)

Answer: Verify that TOTAL_INTERRUPT_SETS is lower or equal to 12.

like image 65
Francis P Avatar answered Sep 07 '25 17:09

Francis P