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
cy_stc_gpio_pin_config_t pinCfg; memset((void *)&pinCfg, 0, sizeof(pinCfg));replaceable withcy_stc_gpio_pin_config_t pinCfg = { 0 };