Menu

Merging both paths of condition (?)

2023-09-15
2023-09-16
  • Martin Poupě

    Martin Poupě - 2023-09-15

    Hello,
    I have following code:

    void g(const char *text);
    void f(const char *text)
    {
        unsigned offset = 0;
        if(!text)
        {
            text = "AA";
        } 
        else if(!strncmp(text,"abcd",4))
        {
                offset = 4;
        }
        g(text + offset);
    }
    

    Both Cppcheck 2.11 and online demo claim, that

    [test.cpp:13]: (portability) Undefined behaviour, pointer arithmetic 'text+offset' is out of bounds.

    If I remove line text = "AA"; problem disappears.
    It looks like cppcheck assumes I use offset 4 for text = "AA".

    Best Regards Martin

     
  • CHR

    CHR - 2023-09-16

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

     

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.