Menu ▾ ▴

Change CWE for containerOutOfBounds and containerOutOfBoundsIndexExpression

Kaixuan Li
2026-11-16
2026-11-16
  • Kaixuan Li

    Kaixuan Li - 2026-11-16

    Hi, I don't have a Trac account, so I'm reporting this here. Could someone please create a ticket? Suggested component: Other (or Check STL). Version: 2.20. Still present on main (e61884466).

    The container out-of-bounds checks report CWE-398 (Indicator of Poor Code Quality), but they report an out-of-bounds memory access, which most checks report as CWE-788. CWE-398 makes these errors look like style findings to tools and users that filter or triage by CWE.

    The CWE is set in lib/checkstl.cpp:

    • containerOutOfBounds: CheckStlImpl::outOfBoundsError, reportError(..., "containerOutOfBounds", ..., CWE398, ...)
    • containerOutOfBoundsIndexExpression: CheckStlImpl::outOfBoundsIndexExpressionError, reportError(..., CWE398, ...)

    Other out-of-bounds checks use CWE-788:

    • stlOutOfBounds (lib/checkstl.cpp, same file) uses CWE788
    • algorithmOutOfBounds (lib/checkstl.cpp, same file) uses CWE788
    • arrayIndexOutOfBounds (lib/checkbufferoverrun.cpp) uses CWE_BUFFER_OVERRUN (788)
    • negativeContainerIndex (lib/checkstl.cpp) uses CWE786 for the underflow case

    Example (test.cpp):

    #include <vector>
    int f1() {
        std::vector<int> v;
        return v[0];
    }
    int f2(const std::vector<int>& v) {
        if (v.size() < 3)
            return 0;
        return v[3];
    }
    int f3() {
        std::vector<int> v(3);
        return v[v.size()];
    }
    int f4() {
        std::vector<int> v(3);
        return v.at(3);
    }
    int g() {
        int a[3] = {0};
        return a[3];
    }
    

    cppcheck --enable=warning --xml --xml-version=2 test.cpp (Cppcheck 2.20.0, built from tag 2.20.1, commit b2606ff31), summarized:

    containerOutOfBounds                 severity=error    cwe=398   v[0], v is empty
    containerOutOfBounds                 severity=warning  cwe=398   v[3] after v.size()<3
    containerOutOfBounds                 severity=error    cwe=398   v[v.size()]
    containerOutOfBounds                 severity=error    cwe=398   v.at(3)
    containerOutOfBoundsIndexExpression  severity=error    cwe=398   v[v.size()]
    arrayIndexOutOfBounds                severity=error    cwe=788   a[3]
    

    The CWE398 value has not changed since containerOutOfBounds was added in 1f427eda8 (2018, "CheckStl: rewrite and refactor out of bounds checker"). The commit does not discuss the CWE, so I think the value was not chosen on purpose.

    A better CWE would be 788 (Access of Memory Location After End of Buffer), the same CWE that stlOutOfBounds, algorithmOutOfBounds and arrayIndexOutOfBounds use. If you want to separate reads from writes, CWE-125 (Out-of-bounds Read) and CWE-787 (Out-of-bounds Write) also fit, but 788 follows the existing convention. Ticket #14168 made a similar change for comparePointers.

     
  • CHR

    CHR - 2026-11-16

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

     

Log in to post a comment.