Search code examples
cruntime-errorstatic-analysismisrafcntl

Polyspace Run-time check alert with C open() function


First, please consider the following piece of code (static function called once from main()):

#define SYSFS_GPIO_DIR                          "/sys/class/gpio"
#define MAX_BUF                                 ((UI_8)64)

typedef uint8_t UI_8
typedef int32_t SI_32
typedef char CHAR_8

static SI_32 ImuGpioFdOpen(UI_8 gpio)
{
    SI_32 fd_gpio_open = -1;
    SI_32 byte_count = -1;
    CHAR_8 aux_buf[MAX_BUF] = {'\0'};

    byte_count = snprintf(aux_buf, sizeof(aux_buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
    if((byte_count > 0) && (byte_count < sizeof(aux_buf))){
        fd_gpio_open = open(aux_buf, O_RDONLY | O_NONBLOCK );
        if(fd_gpio_open < 0){
            syslog (LOG_ERR,"gpio/fd_open");
            fd_gpio_open = ERROR;
        }
    }

    return fd_gpio_open;
}/*ImuGpioFdOpen*/

On the call to open(), static analysis with Polyspace Code Prover raises and alert regarding MISRA's "Dir 4.1 Run-time failures shall be minimized". The alerts says that: "first argument (file path) may not be a valid string"

We don't seem to understand the directive very well, because all our efforts to solve the alerts like this (we have several similar ones) are not yielding results. I mean, we are clearly not building the string correctly, but since the program compiles and runs correctly, we are at a loss.

What kind of run-time check are we missing?

Thank you!

EDIT: I forgot to mention that passing a string literal seems to work for Polyspace, but it doesn't work if we try to pass string generated at runtime (like in the code). Could it be because open()'s prototype declares that the first argument is const char* and Polyspace is taking it too seriously?


Solution

  • The issue has been judged to be a false positive. The alerts shall be justified accordingly.

    Thanks!