Menu

cppcheck 2.0: false positive: uninitvar with (&var)->field

2020-05-13
2020-10-21
  • Michał Kępień

    Hi there,

    Thank you for your work on cppcheck!

    For the following C code:

    struct foo {
        int x;
    };
    
    int main(void) {
        struct foo bar;
        (&bar)->x = 1;
        if (bar.x == 0) { }
        return 0;
    }
    

    cppcheck 2.0 reports:

    Checking test.c ...
    test.c:8:10: error: Uninitialized variable: x [uninitvar]
     if (bar.x == 0) { }
             ^
    

    Using an intermediate variable silences the warning:

    struct foo *baz = &bar;
    baz->x = 1;
    

    So does lack of indirection, of course:

    bar.x = 1;
    

    "git bisect" claims that commit 7368a54629a0174383cedfdb4ec5e4b73475cfa3 is the culprit.

    Please let me know if any further information and/or experiments would help.

     
  • Michał Kępień

    This issue appears to have been fixed by commit 72fa5f2e2729bc8ccf79cfb24ab4ac5a35f9faad, i.e. in cppcheck 2.2.

     

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.