From c99381e1420b3893581ab523ab6ef220816d833d Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Fri, 24 Jul 2026 22:54:19 +0300 Subject: [PATCH] fix: avoid non-linear backtracking in version regex (S8786) 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. --- cpp_linter_hooks/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp_linter_hooks/util.py b/cpp_linter_hooks/util.py index e03c03c..492c620 100644 --- a/cpp_linter_hooks/util.py +++ b/cpp_linter_hooks/util.py @@ -121,8 +121,8 @@ def _detect_installed_version(tool: str) -> Optional[str]: ) except (OSError, subprocess.TimeoutExpired): return None - match = re.search(r"(\d+\.\d+\.\d+(?:\.\d+)?)", result.stdout) - return match.group(1) if match else None + matches = re.findall(r"\d+\.\d+\.\d+(?:\.\d+)?", result.stdout) + return matches[0] if matches else None def _is_version_installed(tool: str, version: str) -> Optional[Path]: