-
Notifications
You must be signed in to change notification settings - Fork 9
Remove obsolete C rule; add C++ static check for UAF and vector issues #177
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
Remove obsolete C rule; add C++ static check for UAF and vector issues #177
Conversation
WalkthroughThis pull request removes an obsolete C rule for C++ pointer returns and introduces new C++ static analysis rules. The new rules detect use-after-free risks when returning pointers from string objects and vector container issues during iteration. Additionally, snapshot files and YAML test cases have been added to validate both the new use-after-free and vector invalidation patterns. Changes
Sequence Diagram(s)sequenceDiagram
participant Dev as Developer
participant Analyzer as Static Analyzer
participant RuleEngine as Rule Engine
participant Warning as Warning System
Dev->>Analyzer: Submit C++ code for analysis
Analyzer->>RuleEngine: Apply new return-pointer & vector rules
RuleEngine->>Analyzer: Return unsafe pattern matches
Analyzer->>Warning: Generate warnings (use-after-free, vector invalidation)
Warning->>Dev: Deliver warnings for code review
Possibly related PRs
Suggested reviewers
Poem
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Sakshis seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (9)
tests/__snapshots__/std-return-data-cpp-snapshot.yml (1)
8-48
: Labeling Details Verification
The labels provide a comprehensive breakdown of the source code elements (e.g., the return statement, variable declarations, and the overall function block). Ensure that the specified byte offsets (start and end values) align exactly with the corresponding code segments.tests/cpp/std-return-data-cpp-test.yml (1)
8-8
: Remove Trailing Whitespace
Line 8 contains trailing spaces. Removing these extra characters will help you pass YAML linting and maintain a clean format.🧰 Tools
🪛 YAMLlint (1.35.1)
[error] 8-8: trailing spaces
(trailing-spaces)
tests/__snapshots__/std-vector-invalidation-cpp-snapshot.yml (1)
4-31
: Label Annotations for Invalidation Patterns
The label annotations effectively highlight critical segments, such as thevec.erase(it)
call and iterator initializations. Double-check that the specified start and end positions match the intended code fragments for clarity.rules/cpp/security/return-c-str-cpp.yml (1)
16-111
: Comprehensive Pattern Matching
The rule block presents an extensive set of patterns that capture multiple variations of returning string internal pointers. Although complex, the nested conditions appear to cover a wide range of cases. Consider adding inline comments or supplementary documentation for future maintainability.rules/cpp/security/std-return-data-cpp.yml (2)
1-7
: Rule Message Typo Correction
There's a minor formatting issue in the warning message. The placeholder for the function name ($FUNC
) is not enclosed in matching backticks. It is currently written as:- $FUNC` returns a pointer to the memory owned by `$VAR`. This pointer + `$FUNC` returns a pointer to the memory owned by `$VAR`. This pointerCorrecting this improves readability and consistency with other rules.
15-86
: Thorough Pattern Matching for Local Container Data Return
This rule is well-crafted to detect return statements that expose data pointers from local containers, which can lead to use-after-free errors. The various nested conditions ensure that only the dangerous patterns are matched. Extensive testing is recommended to ensure the rule avoids false positives in complex codebases.rules/cpp/security/std-vector-invalidation-cpp.yml (2)
4-12
: Clear Warning Message with Detailed ReferencesThe
message
andnote
sections provide detailed guidance and useful references. One minor observation: the note text on line 8 ([CWE-416: Use After Free.
) appears to have an unclosed bracket. Please verify if it should be"[CWE-416: Use After Free]"
for clarity.
38-47
: Duplicate Pattern forresize
DetectedWithin the rule patterns, the pattern for
$CONTAINER.resize($$$)
is defined twice (once on line 42 and again on line 46). To avoid redundant matching, consider removing the duplicate entry.tests/cpp/std-vector-invalidation-cpp-test.yml (1)
91-91
: Remove Trailing SpacesStatic analysis has detected trailing spaces on line 91. Please remove any extraneous whitespace to comply with YAML formatting standards.
🧰 Tools
🪛 YAMLlint (1.35.1)
[error] 91-91: trailing spaces
(trailing-spaces)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (8)
rules/c/security/return-c-str-c.yml
(0 hunks)rules/cpp/security/return-c-str-cpp.yml
(1 hunks)rules/cpp/security/std-return-data-cpp.yml
(1 hunks)rules/cpp/security/std-vector-invalidation-cpp.yml
(1 hunks)tests/__snapshots__/std-return-data-cpp-snapshot.yml
(1 hunks)tests/__snapshots__/std-vector-invalidation-cpp-snapshot.yml
(1 hunks)tests/cpp/std-return-data-cpp-test.yml
(1 hunks)tests/cpp/std-vector-invalidation-cpp-test.yml
(1 hunks)
💤 Files with no reviewable changes (1)
- rules/c/security/return-c-str-c.yml
🧰 Additional context used
🪛 YAMLlint (1.35.1)
tests/cpp/std-return-data-cpp-test.yml
[error] 8-8: trailing spaces
(trailing-spaces)
tests/cpp/std-vector-invalidation-cpp-test.yml
[error] 91-91: trailing spaces
(trailing-spaces)
🔇 Additional comments (12)
tests/__snapshots__/std-return-data-cpp-snapshot.yml (1)
1-7
: Snapshot Definition Validity
The snapshot correctly captures the problematic pattern of returning a pointer to a local vector’s data in the functionreturn_vector_data()
. Please verify that the defined start/end positions (offsets) accurately match the intended snippet.tests/cpp/std-return-data-cpp-test.yml (2)
1-9
: Valid Scenario Test Coverage
The valid test case using theWrapper
class is well defined and correctly returns the data pointer from the member vector. This demonstrates proper usage that should not trigger the error rule.🧰 Tools
🪛 YAMLlint (1.35.1)
[error] 8-8: trailing spaces
(trailing-spaces)
10-15
: Invalid Scenario Test Coverage
The invalid test case—where a function returns a pointer to the data of a locally defined vector—is correctly highlighted. This scenario is expected to trigger the use-after-free rule.tests/__snapshots__/std-vector-invalidation-cpp-snapshot.yml (1)
1-3
: Snapshot for Vector Invalidation Patterns
The snapshot includes a diverse set of loop variants that expose unsafe operations (e.g., erasing an element during iteration). This robust set of examples will greatly aid in testing the new vector invalidation rule.rules/cpp/security/return-c-str-cpp.yml (3)
1-7
: Rule Metadata Clarity
The metadata—including the rule ID, language, severity, and the descriptive warning message—is clearly defined. The message uses placeholders ($FUNC
and$STR
) appropriately. Verify that these placeholders are substituted correctly during analysis.
8-14
: Reference Information Provided
The note section effectively includes CWE references and documentation links, which will help developers understand the potential use-after-free risk when returning internal string pointers.
120-124
: Constraint Definitions Verification
The constraints that restrict the allowed methods (c_str
anddata
) and the string types are correctly formulated, reducing the chance of false positives. Please double-check the regex patterns for any edge case scenarios.rules/cpp/security/std-return-data-cpp.yml (1)
8-13
: Additional Context Provided
The note section effectively provides context regarding the CWE-416 issue and includes references for further reading. This additional context is valuable for developers addressing the warning.rules/cpp/security/std-vector-invalidation-cpp.yml (2)
1-3
: Rule Metadata is Clear and CorrectThe header containing the rule ID, language, and severity follows the expected schema and naming conventions.
15-151
: Comprehensive AST Pattern DefinitionsThe AST pattern definitions are thorough and cover various iterator initialization and update scenarios—including both forward and reverse iterations—while also filtering out safe cases (e.g., when a
for_statement
includes break, continue, return, or goto). For maintainability, you might consider adding inline comments to explain the more complex nested conditions.tests/cpp/std-vector-invalidation-cpp-test.yml (2)
27-91
: Invalid Test Cases Effectively Demonstrate Iterator InvalidationThe invalid test cases for functions
loop_variant_5
throughloop_variant_12
clearly illustrate unsafe iterator practices, such as:
- Not updating the iterator after calling
erase
(e.g., inloop_variant_5
,loop_variant_6
, andloop_variant_9
–loop_variant_10
).- Incorrect usage of reverse iterators (e.g., in
loop_variant_7
,loop_variant_8
,loop_variant_11
, andloop_variant_12
) when passed directly toerase
, which expects a forward iterator.These examples will help ensure that the static analysis rule triggers as expected. Just ensure that these snapshots remain in sync with the rule’s intent.
🧰 Tools
🪛 YAMLlint (1.35.1)
[error] 91-91: trailing spaces
(trailing-spaces)
92-103
: Correct Cross-Container Modification ExampleThe function:
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); } } }demonstrates that modifying a different container (
other_vec
) while iterating overvec
does not raise the vector invalidation error. This example is clear and aligns well with the rule’s intent.
#177) * removed missing-secure-java * httponly-false-csharp * use-of-md5-digest-utils-java * removing use-of-md5-digest-utils and httponly-false-csharp * std-return-data-cpp * std-vector-invalidation-cpp * return-c-str-cpp --------- Co-authored-by: Sakshis <sakshil@abc.com>
Summary by CodeRabbit
New Features
Tests