Skip to content

Commit

Permalink
Add refcounted-usage rule (#714)
Browse files Browse the repository at this point in the history
* Add refcounted-usage rule, also add .mm filetype

* shared_ptr will be another rule
  • Loading branch information
stoletheminerals authored Nov 21, 2024
1 parent 1bcd54a commit 19fa03f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions assets/semgrep_rules/client/dangling-pointer-trait.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ rules:
- "*.h"
- "*.hh"
- "*.hcc"
- "*.mm"
23 changes: 23 additions & 0 deletions assets/semgrep_rules/client/refcounted-usage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ruleid: refcounted-usage
class MyClass : public base::RefCounted<MyClass> {
};

// ruleid: refcounted-usage
class ThreadSafeClass : public base::RefCountedThreadSafe<ThreadSafeClass> {
};

// ruleid: refcounted-usage
base::RefCountedData<int> shared_integer(42);

// ok: refcounted-usage
class RegularClass {
};

// ruleid: refcounted-usage
using MyRefCountedType = base::RefCounted<SomeType>;

// ruleid: refcounted-usage
class NestedRefCounted : public base::RefCountedThreadSafe<NestedRefCounted> {
// ruleid: refcounted-usage
base::RefCountedData<std::string> nested_data_;
};
27 changes: 27 additions & 0 deletions assets/semgrep_rules/client/refcounted-usage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
rules:
- id: refcounted-usage
metadata:
author: Artem Chaikin
source: https://github.com/brave/security-action/blob/main/assets/semgrep_rules/client/refcounted-usage.yaml
assignees: |
stoletheminerals
thypon
cdesouza-chromium
bridiver
pattern-either:
- pattern: base::RefCounted<...>
- pattern: base::RefCountedThreadSafe<...>
- pattern: base::RefCountedData<...>
message: "Reference counting is occasionally useful but is more often a sign that someone isn't thinking carefully about ownership. Use it when ownership is truly shared (for example, multiple tabs sharing the same renderer process), not for when lifetime management is difficult to reason about."
languages:
- generic
paths:
include:
- "*.c"
- "*.cpp"
- "*.cc"
- "*.h"
- "*.hh"
- "*.hcc"
- "*.mm"
severity: WARNING
1 change: 1 addition & 0 deletions assets/semgrep_rules/client/unsafejs-in-cpp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rules:
- "*.h"
- "*.hpp"
- "*.hh"
- "*.mm"
exclude:
- test/
- "*.test.cc"
Expand Down

0 comments on commit 19fa03f

Please sign in to comment.