Menu

False positive due to character array on stack with initialiser

2016-07-01
2016-07-07
  • Daniel Egger

    Daniel Egger - 2016-07-01

    Hi guys,

    I discovered a false positive with some (ancient) use of an initialiser to initialise a character array on the stack to an empty string.

    I tried to create a Trac account to create a ticket but somehow didn't receive the email.

    Here's a demo program to show the warning:

    #include <stdio.h>
    
    int main (int argc, char **argv)
    {
            char str[6] = "\0";
            unsigned short port = 65535;
    
            snprintf (str, sizeof (str), "%hu", port);
    }
    

    The warning is:

    [test.c:8]: (error) Buffer is accessed out of bounds.

    The only way to removing the warning is to get rid of the "\0".

     
  • antred

    antred - 2016-07-04

    The false positive should still be fixed, but why not just do

    char str[6] = {};
    
     

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.