Skip to content

fix: avoid non-linear backtracking in version regex (S8786)#261

Merged
shenxianpeng merged 1 commit into
mainfrom
fix/s8786-regex-backtracking
Jul 24, 2026
Merged

fix: avoid non-linear backtracking in version regex (S8786)#261
shenxianpeng merged 1 commit into
mainfrom
fix/s8786-regex-backtracking

Conversation

@shenxianpeng

@shenxianpeng shenxianpeng commented Jul 24, 2026

Copy link
Copy Markdown
Member

Summary

SonarCloud flagged re.search with a regex containing an optional group pattern as having super-linear backtracking (rule python:S8786).

Change

Replaced re.search + capture group with re.findall in _detect_installed_version(). Same semantics — both return the first version string found — but findall scans linearly without backtracking through the optional (?:\.\d+)? group, which eliminates SonarCloud's concern.

The function returns Optional[str], and findall returns a list of strings, so the .group(1) call is replaced with matches[0]. No behavioral change.

Verification

  • All 35 test_util.py tests pass
  • Ad-hoc verification with 10 cases (3-part, 4-part, empty, no-match, multiple versions) all pass

Issue

Closes SonarCloud issue AZ82F8u7_AXk3ALWpWEQ

Summary by CodeRabbit

  • Bug Fixes
    • Improved detection of installed tool versions from command-line version output.
    • Correctly handles output without a recognizable version by returning no version value.

Replace re.search with re.findall to eliminate the optional-group
backtracking flagged by SonarCloud rule python:S8786. The digit-dot
version regex had a theoretical O(n^2) backtracking path through
the optional (?:.\d+)? group. re.findall scans linearly instead,
fixing the issue with zero behavioral change.
Copilot AI review requested due to automatic review settings July 24, 2026 19:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added the bug Something isn't working label Jul 24, 2026
@sonarqubecloud

Copy link
Copy Markdown

@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ae99f4ad-bb2d-47d7-9f23-3553e98957f0

📥 Commits

Reviewing files that changed from the base of the PR and between 1ee05c3 and c99381e.

📒 Files selected for processing (1)
  • cpp_linter_hooks/util.py

Walkthrough

Changes

Installed version detection

Layer / File(s) Summary
Version output parsing
cpp_linter_hooks/util.py
_detect_installed_version now uses re.findall and returns the first matched version, or None when no version is found.

Estimated code review effort: 1 (Trivial) | ~2 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly reflects the main change: fixing the version regex to avoid non-linear backtracking.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.51%. Comparing base (1ee05c3) to head (c99381e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #261   +/-   ##
=======================================
  Coverage   97.51%   97.51%           
=======================================
  Files           3        3           
  Lines         241      241           
=======================================
  Hits          235      235           
  Misses          6        6           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shenxianpeng
shenxianpeng merged commit 4172722 into main Jul 24, 2026
29 checks passed
@shenxianpeng
shenxianpeng deleted the fix/s8786-regex-backtracking branch July 24, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants