Menu

AST broken by ternary operator + compound literal

NRK
5 days ago
5 days ago
  • NRK

    NRK - 5 days ago

    Minimal reproducer:

    [/tmp]~> cat test.c
    typedef struct { char *s; int len; } Str;
    
    void f(int n)
    {
            Str tmp = {0};
            Str s = n == 0 ? (Str){0} : tmp;
    }
    [/tmp]~> cppcheck test.c 
    Checking test.c ...
    test.c:6:17: error: Syntax Error: AST broken, ternary operator lacks ':'. [internalAstError]
     Str s = n == 0 ? (Str){0} : tmp;
                    ^
    

    Wrapping the compound literal in a paren seems to solve it:

    [/tmp]~> cat test2.c 
    typedef struct { char *s; int len; } Str;
    
    void f(int n)
    {
            Str tmp = {0};
            Str s = n == 0 ? ((Str){0}) : tmp;
    }
    [/tmp]~> cppcheck test2.c
    Checking test2.c ...
    

    Tested with cppcheck version 2.19.1.

     
  • CHR

    CHR - 5 days ago

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14548

     

Log in to post a comment.

MongoDB Logo MongoDB