Menu

false positives after rte_panic

2021-06-14
2021-06-28
  • Paul Aitken

    Paul Aitken - 2021-06-14

    cppcheck doesn't understand that rte_panic terminates execution, so given code like below, "cppcheck --enable=warning" warns:

    "Either the condition '!ptr' is redundant or there is possible null pointer dereference: ptr."

    This is not reported if a "return" is added under the rte_panic.

        ptr = fn();
        if (!ptr) {
            rte_panic("couldn't get ptr");
        }
    
        dostuff(ptr->member, foo, bar);
    
     
  • Daniel Marjamäki

    you can fix that with a configuration file. a start (untested):

    <?xml version="1.0"?>
    <def format="2">
      <function name="rte_panic">
          <noreturn/>
           <arg nr="1">
               <not-uninit/>
           </arg>
        </function>
    </def>
    
     
  • Paul Aitken

    Paul Aitken - 2021-06-14

    Thanks Daniel.

    • <noreturn/> segfaulted.
    • <noreturn>true</noreturn> is parsed, but produces the same output as before: "Either the condition is redundant or there is possible null pointer dereference".
    • <noreturn>false</noreturn> also produces the same output (as expected).

    "--check-library" shows that it's definitely picking up the rte_panic def from the config file.

    (Using cppcheck @ a7707a457)

     

    Last edit: Paul Aitken 2021-06-14
  • Paul Aitken

    Paul Aitken - 2021-06-23

    Any more thoughts on this, @danielmarjamaki ?

     
  • Daniel Marjamäki

    could you give an example code that reproduce the problem

    danielm@debian:~/cppcheck$ ./cppcheck --enable=style 1.c
    Checking 1.c ...
    1.c:2:5: error: syntax error: keyword 'if' is not allowed in global scope [syntaxError]
        if (!ptr) {
        ^
    danielm@debian:~/cppcheck$ cat 1.c
        ptr = fn();
        if (!ptr) {
            rte_panic("couldn't get ptr");
        }
    
        dostuff(ptr->member, foo, bar);
    
     
    • Daniel Marjamäki

      .. and when I try to fill in some code I don't get the false positive.

       
  • Paul Aitken

    Paul Aitken - 2021-06-25

    @danielmarjamaki, use "cppcheck --enable=warning".

    For example:

    void example(void)
    {
        struct passwd *pw = getpwuid(dataplane_uid);
    
        if (!pw)
            rte_panic("could not getpwuid: %s\n", strerror(errno));
    
        getgrouplist(pw->pw_name, dataplane_gid, NULL, &ngroups);
    }
    
    $ cppcheck --enable=warning 1.c
    Checking 1.c ...
    1.c:8:15: warning: Either the condition '!pw' is redundant or there is possible null pointer dereference: pw. [nullPointerRedundantCheck]
     getgrouplist(pw->pw_name, dataplane_gid, NULL, &ngroups);
                  ^
    1.c:5:9: note: Assuming that condition '!pw' is not redundant
        if (!pw) {
            ^
    1.c:8:15: note: Null pointer dereference
     getgrouplist(pw->pw_name, dataplane_gid, NULL, &ngroups);
                  ^
    

    Since rte_panic doesn't return, a null pointer can never be dereferenced.

     
  • Paul Aitken

    Paul Aitken - 2021-06-25

    This works:

       <function name="rte_panic">
         <noreturn>true</noreturn>
         <arg nr="variadic"/>
       </function>
    

    <arg nr="all"/> doesn't work.
    <noreturn/> segfaults.

     
  • Daniel Marjamäki

    it looks like rte_panic takes a format string..

    <?xml version="1.0"?>
    <def format="2">
      <function name="rte_panic">
          <noreturn>true</noreturn>
          <formatstr/>
          <arg nr="1" direction="in">
              <not-uninit/>
              <formatstr/>
          </arg>
        </function>
    </def>
    

    That solves the false positive for me.

     
  • Paul Aitken

    Paul Aitken - 2021-06-26

    That's even better, thanks @danielmarjamaki.

    Would you add that to one of the existing cfg files, or start a new one for DPDK APIs?

     
  • Daniel Marjamäki

    I would probably create a new dpdk.cfg file.. if you create that and share it with me I will add it in Cppcheck.

     

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.