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.
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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) usesCWE788algorithmOutOfBounds(lib/checkstl.cpp, same file) usesCWE788arrayIndexOutOfBounds(lib/checkbufferoverrun.cpp) usesCWE_BUFFER_OVERRUN(788)negativeContainerIndex(lib/checkstl.cpp) usesCWE786for the underflow caseExample (test.cpp):
cppcheck --enable=warning --xml --xml-version=2 test.cpp(Cppcheck 2.20.0, built from tag 2.20.1, commit b2606ff31), summarized:The CWE398 value has not changed since
containerOutOfBoundswas 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,algorithmOutOfBoundsandarrayIndexOutOfBoundsuse. 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.Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/15223