Menu

Cppcheck 2.2 FalsePositive Null pointer dereference

Matt Burt
2020-10-05
2020-10-13
  • Matt Burt

    Matt Burt - 2020-10-05

    Following program illustrates the issue (OK with 2.1):-

    #include <stdlib.h>
    #include <stdio.h>
    
    struct xfs_fs
    {
        char **inode_table;
    };
    
    /*  ------------------------------------------------------------------------ */
    static int
    alloc_xfs(struct xfs_fs *xfs)
    {
        int result = 0;
        char **new_table = malloc(4096 * sizeof(new_table[0]));
    
        if (new_table != NULL)
        {
            xfs->inode_table = new_table;
    
            result = 1;
        }
    
        return result;
    }
    
    /*  ------------------------------------------------------------------------ */
    int main()
    {
        struct xfs_fs *xfs = malloc(sizeof(struct xfs_fs));
    
        xfs->inode_table = 0;
    
        if (alloc_xfs(xfs))
        {
            xfs->inode_table[0] = NULL;
            xfs->inode_table[1] = "entry 1";
            xfs->inode_table[2] = "entry 2";
    
            printf("Succeeded!\n");
        }
    }
    

    cppcheck 2.2 complains about the allocations to xfs->inode_table.

    Unless I'm missing something obvious, alloc_xfs() only returns non-zero when the assignments to xfs_inode_table are allowed.

     
  • Matt Burt

    Matt Burt - 2020-10-06

    The Z3 version incidentally is 4.4.1. I don't know if this makes a difference or not.

     
  • Matt Burt

    Matt Burt - 2020-10-13

    Sorry to be pushy, but could I bump this?

    This is distilled from a source file on the XRDP project [github.com] which I help to maintain.

    Many thanks.

     

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.