Menu

cppcheck complains on "Shifting by a negative value" on valid usage under if condition of the operand

Tal
2024-11-18
2024-11-19
  • Tal

    Tal - 2024-11-18

    The following code should be valid, but cppcheck complains no matter of if with constexpr or a regular if:

    template <int b>
    int f(int a)
    {
        if constexpr (b >= 0) {
            return a << b;
        } else {
            return a << -b;
        }
    }
    
    int g() {
        return f<1>(2)
    }
    

    Yields both in my personal code and in http://cppcheck.net/demo/:

    [test.cpp:7]: (error) Shifting by a negative value is undefined behaviour
    

    CPPCheck should take into account the branching over the right operand (here, b) of the << operator.

     
  • Tal

    Tal - 2024-11-18

    Sadly this is the duplicated post, please visit the original post

     
  • CHR

    CHR - 2024-11-19

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

     
    👍
    1

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.