Menu

FP uninitvar on getsockopt()

7 days ago
6 days ago
  • David Gibson

    David Gibson - 7 days ago

    I just encountered an odd false positive on uninitvar. cppcheck is detecting an variable as uninitialized, although it has been set as the result of a getsockopt(). Specifically, with the attached example file:

    $ cppcheck --enable=all --library=posix --suppress=missingIncludeSystem --suppress=unusedStructMember --suppress=unusedFunction --include=/usr/include/linux/tcp.h cppcheck-getsockopt-uninitvar.c
    Checking cppcheck-getsockopt-uninitvar.c ...
    Checking cppcheck-getsockopt-uninitvar.c: __BIG_ENDIAN_BITFIELD...
    cppcheck-getsockopt-uninitvar.c:21:2: warning: Assignment of function parameter has no effect outside the function. Did you forget dereferencing it? [uselessAssignmentPtrArg]
     tinfo = &tinfo_new;
     ^
    cppcheck-getsockopt-uninitvar.c:19:24: warning: Uninitialized variable: tinfo->tcpi_retransmits [uninitvar]
     printf("%u\n", tinfo->tcpi_retransmits);
                           ^
    cppcheck-getsockopt-uninitvar.c:13:6: note: Assuming condition is false
     if (!tinfo) {
         ^
    cppcheck-getsockopt-uninitvar.c:19:24: note: Uninitialized variable: tinfo->tcpi_retransmits
     printf("%u\n", tinfo->tcpi_retransmits);
                           ^
    Checking cppcheck-getsockopt-uninitvar.c: __LITTLE_ENDIAN_BITFIELD...
    nofile:0:0: information: Unmatched suppression: unusedStructMember [unmatchedSuppression]
    
    nofile:0:0: information: Active checkers: 110/966 (use --checkers-report=<filename> to see details) [checkersReport]
    

    The FP appears to be quite sensitive to the particulars:
    * Removing the final pointless assignment of tinfo (correctly warned by cppcheck) removes the uninitvar warning
    * Setting tinfo = NULL rather than passing as a parameter removes the warning

     

    Last edit: David Gibson 6 days ago
  • David Gibson

    David Gibson - 7 days ago

    Sorry, forgot to mention, this occurred with cppcheck 2.18.3 (installed from Fedora package cppcheck-2.18.3-1.fc42.x86_64).

     
  • CHR

    CHR - 7 days ago

    Does this happen without --include=/usr/include/linux/tcp.h?

     
    • David Gibson

      David Gibson - 6 days ago

      It does not. It does appear if I replace the --include with -I /usr/include so cppcheck can find the header that way. That also triggers a bunch of other warnings from headers, though.

      The warning also appears if I don't include that header, but locally define struct tcp_info. The bug originally appeared that way, but it's a large structure, so I used the header for brevity,

      The warning does not appear if I use the version of the structure defined in /usr/include/netinet/tcp.h instead of the one in the Linux header. The Linux one is different, because it includes extension fields. I originally found the problem in an example using one of those extension fields, but the warning persists if I use one of the base fields that's common between the POSIX and Linux versions.

       

      Last edit: David Gibson 6 days ago
  • CHR

    CHR - 6 days ago

    Please include the struct definition that reproduces the error.

     

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.