codeql-coding-standards icon indicating copy to clipboard operation
codeql-coding-standards copied to clipboard

`A4-7-1`: integer data loss false positive erasing from container

Open fjatWbyT opened this issue 1 year ago • 1 comments

Affected rules

  • A4-7-1

Description

Decrement cannot lead to wrap-around since the container is checked for emptiness.

Example

class container
{
    bool empty()
    {
        return num_items_ == 0;
    }

    void erase()
    {
        if (empty())
            return;

        num_items_--;
    }

    std::size_t num_items_ = 0;
};

fjatWbyT avatar Nov 11 '24 00:11 fjatWbyT

This could be addressed by adopting the CERT C implementation for this rule (as per https://github.com/github/codeql-coding-standards/issues/491), then extending support for container implementations.

lcartey avatar Dec 10 '24 11:12 lcartey