Skip to content

Commit eb09931

Browse files
committed
fix: SonarCloud quality gate — regex DoS, dict fromkeys, vulture whitelist
1 parent b651760 commit eb09931

3 files changed

Lines changed: 32 additions & 23 deletions

File tree

src/treemapper/diffctx/edges/semantic/rust.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ...types import Fragment, FragmentId
99
from ..base import EdgeBuilder, EdgeDict
1010

11-
_RUST_USE_STMT_RE = re.compile(r"^\s*use\s+(.+?)\s*;", re.MULTILINE)
11+
_RUST_USE_STMT_RE = re.compile(r"^\s*use\s+([^;\n]+?)\s*;", re.MULTILINE)
1212
_RUST_MOD_RE = re.compile(r"^\s*(?:pub(?:\([^)]*\))?\s+)?mod\s+([a-z_][a-z0-9_]*)\s*[;{]", re.MULTILINE)
1313

1414
_RUST_FN_RE = re.compile(r"^\s*(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?fn\s+([a-z_][a-z0-9_]*)", re.MULTILINE)
@@ -126,45 +126,54 @@ def _is_rust_file(path: Path) -> bool:
126126
_MAX_USE_TREE_DEPTH = 10
127127

128128

129-
def _parse_use_tree(text: str, _depth: int = 0) -> list[str]:
130-
if _depth > _MAX_USE_TREE_DEPTH:
131-
return []
132-
text = re.sub(r"^(?:crate|self|super)::", "", text.strip())
133-
if "{" not in text:
134-
return [text] if text else []
135-
brace_pos = text.index("{")
136-
prefix = text[:brace_pos].rstrip(":")
137-
inner = text[brace_pos + 1 :]
129+
def _find_matching_brace(inner: str) -> int:
138130
depth = 1
139-
end = 0
140131
for i, ch in enumerate(inner):
141132
if ch == "{":
142133
depth += 1
143134
elif ch == "}":
144135
depth -= 1
145136
if depth == 0:
146-
end = i
147-
break
148-
items_str = inner[:end]
149-
results: list[str] = []
137+
return i
138+
return 0
139+
140+
141+
def _split_brace_items(items_str: str) -> list[str]:
142+
items: list[str] = []
150143
current: list[str] = []
151-
d = 0
144+
depth = 0
152145
for ch in items_str:
153146
if ch == "{":
154-
d += 1
147+
depth += 1
155148
current.append(ch)
156149
elif ch == "}":
157-
d -= 1
150+
depth -= 1
158151
current.append(ch)
159-
elif ch == "," and d == 0:
152+
elif ch == "," and depth == 0:
160153
item = "".join(current).strip()
161154
if item and item != "self":
162-
results.extend(_parse_use_tree(f"{prefix}::{item}" if prefix else item, _depth + 1))
155+
items.append(item)
163156
current = []
164157
else:
165158
current.append(ch)
166159
item = "".join(current).strip()
167160
if item and item != "self":
161+
items.append(item)
162+
return items
163+
164+
165+
def _parse_use_tree(text: str, _depth: int = 0) -> list[str]:
166+
if _depth > _MAX_USE_TREE_DEPTH:
167+
return []
168+
text = re.sub(r"^(?:crate|self|super)::", "", text.strip())
169+
if "{" not in text:
170+
return [text] if text else []
171+
brace_pos = text.index("{")
172+
prefix = text[:brace_pos].rstrip(":")
173+
inner = text[brace_pos + 1 :]
174+
end = _find_matching_brace(inner)
175+
results: list[str] = []
176+
for item in _split_brace_items(inner[:end]):
168177
results.extend(_parse_use_tree(f"{prefix}::{item}" if prefix else item, _depth + 1))
169178
return results
170179

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ def _write_score_histogram(terminalreporter, results):
733733
return
734734

735735
bucket_labels = ["0-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-100", "100"]
736-
counts = {b: 0 for b in bucket_labels}
736+
counts = dict.fromkeys(bucket_labels, 0)
737737
for s in scores:
738738
if s >= 100.0:
739739
counts["100"] += 1

whitelist_vulture.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
is_nlp_available
3636
Graph.add_node
3737
QuotientNode.fragment_count
38-
ProjectGraph.edges_of_type # NOSONAR(python:S905)
39-
ProjectGraph.subgraph # NOSONAR(python:S905)
38+
_ = ProjectGraph.edges_of_type
39+
_ = ProjectGraph.subgraph
4040
AnsibleEdgeBuilder
4141
BazelEdgeBuilder
4242
CargoEdgeBuilder

0 commit comments

Comments
 (0)