Skip to content

fnmatch: replace recursive matcher with iterative greedy algorithm - #22911

Closed
MegaManSec wants to merge 1 commit into
curl:masterfrom
MegaManSec:worktree-agent-a9e3ffe051cb25e36
Closed

MegaManSec wants to merge 1 commit into
curl:masterfrom
MegaManSec:worktree-agent-a9e3ffe051cb25e36

Conversation

@MegaManSec

@MegaManSec MegaManSec commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

The fallback FTP wildcard matcher recursively tried pattern suffixes at every possible string position. A non-matching pattern such as 'ab' against a long string could therefore take O(pattern length * string length²) work.

Replace the recursion with an iterative greedy matcher that backtracks only to the most recent '*', reducing the bound to O(pattern length * string length). Character classes, escapes, '?', and the existing two-star-group limit retain their behavior.

Unit test 1307 covers the preserved star-group limit.

@github-actions github-actions Bot added the FTP label Sep 11, 2026
The fallback matcher did recursive backtracking and could do O(pattern length * string length^2) work for a non-match. Use iterative greedy matching that backtracks only to the most recent '*', reducing the bound to O(pattern length * string length). Preserve the existing wildcard syntax and two-star-group limit.
@MegaManSec
MegaManSec force-pushed the worktree-agent-a9e3ffe051cb25e36 branch from 24e04b3 to e179a2a Compare September 11, 2026 06:54
@github-actions github-actions Bot added the tests label Sep 11, 2026
@bagder
bagder requested a balanced review from Copilot September 11, 2026 13:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The implementation preserves tested behavior while removing recursive quadratic backtracking.

Pull request overview

Replaces recursive FTP wildcard matching with bounded iterative backtracking while preserving existing semantics.

Changes:

  • Adds token-level matching and iterative * handling.
  • Tests the two-star-group limit across implementations.
File summaries
File Description
lib/curl_fnmatch.c Implements the iterative matcher.
tests/unit/unit1307.c Adds star-group-limit coverage.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@bagder bagder closed this in 997f66e Sep 11, 2026
@MegaManSec
MegaManSec deleted the worktree-agent-a9e3ffe051cb25e36 branch September 14, 2026 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants