1

I have the gpio initialization code (sample below). It is compiled without any issues and is executing properly.

typedef enum {
    USER_LED = 0,
    DEBUG_UART_TX,
    DEBUG_UART_RX,
    TOTAL_IO_PINS,
} gpio_pin;

typedef struct
{
    GPIO_PRT_Type *port;
    uint32_t       pin;
    uint8_t        drive_mode;
    uint8_t        hsio; // high speed io matrix/multiplexer
} st_gpioconfig;

st_gpioconfig gpio_configuration_array[] = {
    { GPIO_PRT13, P13_7_PIN, CY_GPIO_DM_STRONG_IN_OFF, HSIOM_SEL_GPIO },
    { GPIO_PRT5, P5_1_PIN, CY_GPIO_DM_STRONG_IN_OFF, P5_1_SCB5_UART_TX },
    { GPIO_PRT5, P5_0_PIN, CY_GPIO_DM_HIGHZ, P5_0_SCB5_UART_RX },
};

void gpio_init()
{

    cy_stc_gpio_pin_config_t pinCfg;
    memset((void *)&pinCfg, 0, sizeof(pinCfg));

    for (int i = 0; i < TOTAL_IO_PINS ; i++) {

        pinCfg.driveMode = gpio_configuration_array[i].drive_mode;
        pinCfg.hsiom = gpio_configuration_array[i].hsio;
        Cy_GPIO_Pin_Init(gpio_configuration_array[i].port, gpio_configuration_array[i].pin,
                         &pinCfg);
    }
}

But when static analysis with Cppcheck is performed, the below mentioned errors are thrown

[src/gpio.c:46]: (error) Array 'gpio_configuration_array[1]' accessed at index 2, which is out of bounds.
[src/gpio.c:47]: (error) Array 'gpio_configuration_array[1]' accessed at index 2, which is out of bounds.
[src/gpio.c:48]: (error) Array 'gpio_configuration_array[1]' accessed at index 2, which is out of bounds.

The error is fixed with the below change

**st_gpioconfig gpio_configuration_array[TOTAL_IO_PINS]**

Any experts on CPPCHECK can shed some light on - what is happening here?

Update - I am using - Cppcheck 1.84

2
  • Aside: cy_stc_gpio_pin_config_t pinCfg; memset((void *)&pinCfg, 0, sizeof(pinCfg)); replaceable with cy_stc_gpio_pin_config_t pinCfg = { 0 }; Commented Jul 7, 2024 at 3:09
  • I am not able to reproduce the false positives with any released version of Cppcheck. Also your version of Cppcheck is seriously outdated. Please refer to github.com/danmar/cppcheck?tab=readme-ov-file#packages on how to obtain the latest version. Commented Jan 9 at 16:56

1 Answer 1

2

This seems a false positive from Cppcheck. May be, Cppcheck is not able to compute the size of array, like the way compiler computes it for us, based on the size of initializer, when we omit the dimension.

When array initialized and dimension omitted in definition, it seems, Cppcheck assumes size of array size as 1 and end up reporting error when it finds access of any element of that array other than 1st element.

Found a few post related to false positive array index out of bounds reported by Cppcheck:

It would be good to provide the version of Cppcheck, that you are using, in your post.
Also, I would suggest, write a sample program and try a few things, like move array definition inside the function, use sizeof (gpio_configuration_array) / sizeof (gpio_configuration_array[0]) to compute size of array in for loop condition etc. and check the behavior of Cppcheck. To report an issue in Cppcheck, you can initiate discussion here - cppcheck discussion.

The developer of Cppcheck is also a contributor in SO - Daniel Marjamäki. Hope, he will provide some comment on your post.

Sign up to request clarification or add additional context in comments.

2 Comments

Cppcheck assumes size of array size as 1 and end up reporting error when it finds access of any element of that array other than 1st element. But then shouldn't it also be emitting error messages with accessed at index 1?
@Andrew Henle Cppcheck is a static code analysis tool. May be, when it sees the array access in for loop body where loop variable used as array index and finds the loop conditioni < TOTAL_IO_PINS, which when executed, as per it's analysis of array size, will end up accessing array beyond it's size, so, it reported index TOTAL_IO_PINS - 1 in the error as it's the maximum index of array that would be accessed in the loop during execution. This is just a speculation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.