Menu

One AST failure, one false positive when testing our codebase.

2021-11-24
2021-11-24
  • Sebastian Andersson

    I tried cppcheck 2.6.2 (and c7ef602cd6d58af2ef3d5a7ec01b53dded03cc05) on our C codebase and got these two problems. Perhaps someone with access to be bug tracker can add them there?

    #include <stdint.h>
    
    int func(void)
    {
        static uint32_t v;
    
        if (++v == 0) {
            ++v;
            return 1;
        }
        return 0;
    }
    

    It gives the following output:

     test.c:7:13: style: Condition '++v==0' is always false [knownConditionTrueFalse]
        if (++v == 0) {
                ^
    

    This is perhaps a variant of #10248 ?

    A function converting one enum to another in a creative way fails with a broken AST error:

    typedef enum {
        A,
        B,
        C
    } enum1_t;
    
    typedef enum {
        D,
        E,
        F
    } enum2_t;
    
    enum2_t func1(enum1_t v)
    {
        return (enum2_t[]) {
            [A] = D,
            [B] = E,
            [C] = F
        }[v];
    }
    
    enum2_t func2(enum1_t v)
    {
        enum2_t ret;
        ret = (enum2_t[]) {
            [A] = D,
            [B] = E,
            [C] = F
        }[v];
        return ret;
    }
    

    func1 works fine, func2 fails with:

    test2.c:25:9: error: Syntax Error: AST broken, binary operator '=' doesn't have two operands. [internalAstError]
        ret = (enum2_t[]) {
            ^
    
     
  • CHR

    CHR - 2021-11-24

    In the first case, you are relying on unsigned overflow, right?
    The second example should be covered by https://trac.cppcheck.net/ticket/10177

     
    • Sebastian Andersson

      Yes, its with unsigned overflow.

      I missed 10177 when I searched for it, sorry about that.

       
  • CHR

    CHR - 2021-11-24

    No worries. Here is a ticket for the first issue: https://trac.cppcheck.net/ticket/10616

     

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.