-
Notifications
You must be signed in to change notification settings - Fork 9
Rules- std-vector-invalidation - c/cpp #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
petrisorcoderabbit
merged 4 commits into
coderabbitai:main
from
ESS-ENN:std-vector-invalidation-c/cpp
Oct 21, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
id: std-vector-invalidation-c | ||
language: c | ||
severity: warning | ||
message: >- | ||
Modifying an `std::vector` while iterating over it could cause the | ||
container to reallocate, triggering memory corruption. | ||
note: >- | ||
[CWE-416: Use After Free. | ||
[REFERENCES] | ||
- https://wiki.sei.cmu.edu/confluence/display/c/MEM30-C.+Do+not+access+freed+memory | ||
- https://wiki.sei.cmu.edu/confluence/display/cplusplus/EXP54-CPP.+Do+not+access+an+object+outside+of+its+lifetime | ||
rule: | ||
kind: call_expression | ||
all: | ||
- pattern: $CONTAINER.$R($$$) | ||
inside: | ||
stopBy: end | ||
kind: for_statement | ||
any: | ||
- pattern: for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.begin(); $IT!= $CONTAINER.end(); ++$IT){$$$} | ||
- pattern: for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.begin(); $IT!= $CONTAINER.end(); $IT++){$$$} | ||
- pattern: | ||
for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.rbegin(); $IT!= $CONTAINER.rend(); ++$IT) | ||
{$$$} | ||
- pattern: | ||
for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.rbegin(); $IT!= $CONTAINER.rend(); $IT++) | ||
{$$$} | ||
- pattern: for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.begin(),$IT_END = $CONTAINER.end(); $IT !=$IT_END; ++$IT){$$$} | ||
- pattern: for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.begin(), | ||
$IT_END = $CONTAINER.end(); $IT != $IT_END; $IT++){$$$} | ||
- pattern: for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.rbegin(), | ||
$IT_END = $CONTAINER.rend(); $IT != $IT_END; ++$IT){$$$} | ||
- pattern: for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.rbegin(), | ||
$IT_END = $CONTAINER.rend(); $IT != $IT_END; $IT++){$$$} | ||
- inside: | ||
not: | ||
kind: assignment_expression | ||
all: | ||
- has: | ||
stopBy: neighbor | ||
kind: identifier | ||
pattern: $IT | ||
- has: | ||
stopBy: neighbor | ||
regex: "^=$" | ||
- has: | ||
stopBy: neighbor | ||
kind: call_expression | ||
pattern: $CONTAINER.$R($IT) | ||
ESS-ENN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
constraints: | ||
R: | ||
regex: "^erase|assign|clear|insert|resize|push_back|reserve|shrink_to_fit|resize|pop_back$" | ||
ESS-ENN marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
id: std-vector-invalidation-cpp | ||
language: cpp | ||
severity: warning | ||
message: >- | ||
Modifying an `std::vector` while iterating over it could cause the | ||
container to reallocate, triggering memory corruption. | ||
note: >- | ||
[CWE-416: Use After Free. | ||
[REFERENCES] | ||
- https://wiki.sei.cmu.edu/confluence/display/c/MEM30-C.+Do+not+access+freed+memory | ||
- https://wiki.sei.cmu.edu/confluence/display/cplusplus/EXP54-CPP.+Do+not+access+an+object+outside+of+its+lifetime | ||
rule: | ||
kind: call_expression | ||
all: | ||
- pattern: $CONTAINER.$R($$$) | ||
inside: | ||
stopBy: end | ||
kind: for_statement | ||
any: | ||
- pattern: for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.begin(); $IT!= $CONTAINER.end(); ++$IT){$$$} | ||
- pattern: for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.begin(); $IT!= $CONTAINER.end(); $IT++){$$$} | ||
- pattern: | ||
for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.rbegin(); $IT!= $CONTAINER.rend(); ++$IT) | ||
{$$$} | ||
- pattern: | ||
for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.rbegin(); $IT!= $CONTAINER.rend(); $IT++) | ||
{$$$} | ||
- pattern: for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.begin(),$IT_END = $CONTAINER.end(); $IT !=$IT_END; ++$IT){$$$} | ||
- pattern: for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.begin(), | ||
$IT_END = $CONTAINER.end(); $IT != $IT_END; $IT++){$$$} | ||
- pattern: for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.rbegin(), | ||
$IT_END = $CONTAINER.rend(); $IT != $IT_END; ++$IT){$$$} | ||
- pattern: for(std::vector<$TY>::$IT_TYPE $IT = $CONTAINER.rbegin(), | ||
$IT_END = $CONTAINER.rend(); $IT != $IT_END; $IT++){$$$} | ||
- inside: | ||
not: | ||
kind: assignment_expression | ||
all: | ||
- has: | ||
stopBy: neighbor | ||
kind: identifier | ||
pattern: $IT | ||
- has: | ||
stopBy: neighbor | ||
regex: "^=$" | ||
- has: | ||
stopBy: neighbor | ||
kind: call_expression | ||
pattern: $CONTAINER.$R($IT) | ||
|
||
constraints: | ||
R: | ||
regex: "^erase|assign|clear|insert|resize|push_back|reserve|shrink_to_fit|resize|pop_back$" |
22 changes: 22 additions & 0 deletions
22
tests/__snapshots__/std-vector-invalidation-c-snapshot.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
id: std-vector-invalidation-c | ||
snapshots: | ||
? "void loop_variant_5(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_6(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_7(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.rbegin(); it != vec.rend(); ++it) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_8(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.rbegin(); it != vec.rend(); it++) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_9(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.begin(), end = vec.end(); it != end; ++it) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_10(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.begin(), end = vec.end(); it != end; it++) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_11(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.rbegin(), end = vec.rend(); it != end; ++it) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_12(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.rbegin(), end = vec.rend(); it != end; it++) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n} \nvoid f(std::vector<int> &vec, std::vector<int> &other_vec) {\nfor(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {\nif (foo()) {\n // ruleid: std-vector-invalidation\n vec.push_back(0);\n\n // Modifying a different container is OK\n // ok: std-vector-invalidation\n other_vec.push_back(0);\n}\n}\n}\n" | ||
: labels: | ||
- source: vec.erase(it) | ||
style: primary | ||
start: 183 | ||
end: 196 | ||
- source: |- | ||
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
style: secondary | ||
start: 45 | ||
end: 201 | ||
- source: vec.erase(it); | ||
style: secondary | ||
start: 183 | ||
end: 197 |
22 changes: 22 additions & 0 deletions
22
tests/__snapshots__/std-vector-invalidation-cpp-snapshot.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
id: std-vector-invalidation-cpp | ||
snapshots: | ||
? "void loop_variant_5(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_6(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_7(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.rbegin(); it != vec.rend(); ++it) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_8(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.rbegin(); it != vec.rend(); it++) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_9(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.begin(), end = vec.end(); it != end; ++it) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_10(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.begin(), end = vec.end(); it != end; it++) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_11(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.rbegin(), end = vec.rend(); it != end; ++it) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n}\nvoid loop_variant_12(std::vector<int> &vec) {\nfor(std::vector<int>::iterator it = vec.rbegin(), end = vec.rend(); it != end; it++) {\nif (should_erase(*it)) {\n // ruleid: std-vector-invalidation\n vec.erase(it);\n}\n}\n} \nvoid f(std::vector<int> &vec, std::vector<int> &other_vec) {\nfor(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {\nif (foo()) {\n // ruleid: std-vector-invalidation\n vec.push_back(0);\n\n // Modifying a different container is OK\n // ok: std-vector-invalidation\n other_vec.push_back(0);\n}\n}\n}\n" | ||
: labels: | ||
- source: vec.erase(it) | ||
style: primary | ||
start: 183 | ||
end: 196 | ||
- source: |- | ||
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
style: secondary | ||
start: 45 | ||
end: 201 | ||
- source: vec.erase(it); | ||
style: secondary | ||
start: 183 | ||
end: 197 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
id: std-vector-invalidation-c | ||
valid: | ||
- | | ||
void f(std::vector<int> &vec) { | ||
for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) { | ||
if (should_erase(*it)) { | ||
// This is the correct way to iterate while erasing | ||
// ok: std-vector-invalidation | ||
it = vec.erase(it); | ||
} else { | ||
++it; | ||
} | ||
} | ||
} | ||
bool isInList(const TCHAR *token2Find, std::vector<int> ¶ms, bool eraseArg = true) | ||
{ | ||
for (std::vector<int>::iterator = params.begin(); it != params.end(); ++it) | ||
{ | ||
if (lstrcmp(token2Find, it->c_str()) == 0) | ||
{ | ||
// ok: std-vector-invalidation | ||
if (eraseArg) params.erase(it); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
invalid: | ||
- | | ||
void loop_variant_5(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_6(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_7(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.rbegin(); it != vec.rend(); ++it) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_8(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.rbegin(); it != vec.rend(); it++) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_9(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.begin(), end = vec.end(); it != end; ++it) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_10(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.begin(), end = vec.end(); it != end; it++) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_11(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.rbegin(), end = vec.rend(); it != end; ++it) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_12(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.rbegin(), end = vec.rend(); it != end; it++) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void f(std::vector<int> &vec, std::vector<int> &other_vec) { | ||
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) { | ||
if (foo()) { | ||
// ruleid: std-vector-invalidation | ||
vec.push_back(0); | ||
|
||
// Modifying a different container is OK | ||
// ok: std-vector-invalidation | ||
other_vec.push_back(0); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
id: std-vector-invalidation-cpp | ||
valid: | ||
- | | ||
void f(std::vector<int> &vec) { | ||
for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) { | ||
if (should_erase(*it)) { | ||
// This is the correct way to iterate while erasing | ||
// ok: std-vector-invalidation | ||
it = vec.erase(it); | ||
} else { | ||
++it; | ||
} | ||
} | ||
} | ||
bool isInList(const TCHAR *token2Find, std::vector<int> ¶ms, bool eraseArg = true) | ||
{ | ||
for (std::vector<int>::iterator = params.begin(); it != params.end(); ++it) | ||
{ | ||
if (lstrcmp(token2Find, it->c_str()) == 0) | ||
{ | ||
// ok: std-vector-invalidation | ||
if (eraseArg) params.erase(it); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
invalid: | ||
- | | ||
void loop_variant_5(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_6(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_7(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.rbegin(); it != vec.rend(); ++it) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_8(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.rbegin(); it != vec.rend(); it++) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_9(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.begin(), end = vec.end(); it != end; ++it) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_10(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.begin(), end = vec.end(); it != end; it++) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_11(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.rbegin(), end = vec.rend(); it != end; ++it) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void loop_variant_12(std::vector<int> &vec) { | ||
for(std::vector<int>::iterator it = vec.rbegin(), end = vec.rend(); it != end; it++) { | ||
if (should_erase(*it)) { | ||
// ruleid: std-vector-invalidation | ||
vec.erase(it); | ||
} | ||
} | ||
} | ||
void f(std::vector<int> &vec, std::vector<int> &other_vec) { | ||
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) { | ||
if (foo()) { | ||
// ruleid: std-vector-invalidation | ||
vec.push_back(0); | ||
|
||
// Modifying a different container is OK | ||
// ok: std-vector-invalidation | ||
other_vec.push_back(0); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.