Menu

[containerOutOfBounds] false positive after bounds-check

DougCube
6 days ago
5 days ago
  • DougCube

    DougCube - 6 days ago

    I think this is a false positive.

    class Foo {
        static constexpr size_t DEPTH = 4;
        std::array<T, DEPTH+1> arr;
    
        [[noreturn]] my_exit() { exit(1); }
    public:
        const T* func(const unsigned int n) const {
            if(n > DEPTH)
                my_exit();
            return &arr[n];
        }
    }
    

    warning: Either the condition 'n>DEPTH' is redundant or 'n' can have the
    value 4. Expression 'arr[n]' cause access out of bounds.
    [containerOutOfBounds] return &arr[n]; ^

    There might also be a typo in the error message as here the array index can
    range from 0 to 4, and it says "can have the value 4" instead of ">4".

    But I changed it and it still doesn't pass:

        const T* func(const unsigned int n) const {
            if(n > DEPTH)
                return nullptr;
            return &arr[n];
        }
    

    -Doug

     

    Last edit: DougCube 5 days ago
  • CHR

    CHR - 6 days ago

    I can't reproduce this, which version are you using?
    Also, please add ~~~ around code.

     
    • DougCube

      DougCube - 5 days ago

      2.13.0

       
  • DougCube

    DougCube - 5 days ago

    I switched to version 2.19.0 and this issue is fixed.

     
    👍
    1

Log in to post a comment.