Wildcard matcher backtracks exponentially in the number of `*`: a 200-char name with an 11-char pattern takes ~10 s

Aperta
#302 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
cli, performance

Direzione di ricerca

Read CPP/Common/Wildcard.cpp, especially EnhancedMaskTest() around lines 124–153, and trace how * and ? are matched. Reproduce the slow case with the reported command and pattern, then compare behavior across wildcard cases. Done means matching preserves existing wildcard results and the 200-character no-match case no longer grows exponentially with each *.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Environment
  • 7-Zip (z) 26.06 (arm64), ip7z/7zip @ d3296e3, built with CPP/7zip/cmpl_mac_arm64.mak (Bundles/Alone2)
  • macOS arm64, Apple clang
Description

Matching a file name against a pattern with several * takes time proportional to roughly n^k (n = name length, k = number of *). With a 200-character name and the 11-character pattern *a*a*a*a*ab (no match) the command runs about 8-11 s. Each extra *a multiplies the time by about 35.

Steps to reproduce
Z=7zz
d=$(mktemp -d) && cd "$d"
touch "$(python3 -c "print('a'*200)")"
for k in 3 4 5; do
  pat=$(python3 -c "print('*a'*$k+'b')")
  s=$(python3 -c 'import time;print(time.time())')
  $Z h -bd "$pat" | grep -E '^Files:'
  echo "k=$k pattern=$pat elapsed=$(python3 -c "import time;print(round(time.time()-$s,2))")s"
done

Observed (two runs):

Files: 0
k=3 pattern=*a*a*ab elapsed=0.06s
Files: 0
k=4 pattern=*a*a*a*ab elapsed=0.25s
Files: 0
k=5 pattern=*a*a*a*a*ab elapsed=8.04s     (second run: 8.01 s)

The same cost appears with -i!pattern, -x!pattern, an @listfile line, and for l, t, x, d, a.

Expected

The command line documentation defines the wildcards only by meaning:

7-Zip uses wild name matching similar to Windows 95:
'*' means a sequence of arbitrary characters.
'?' means any character.

(https://7-zip.opensource.jp/chm/cmdline/syntax.htm). It gives no complexity statement, but matching one short name against one short pattern should finish in time polynomial in a small degree, not in the number of *.

Actual

Exponential-in-k run time, rc=0 and no output (see above). k=6 would take several minutes.

Root cause (analysis on d3296e3)

CPP/Common/Wildcard.cpp:124-153, EnhancedMaskTest(). At a * (line 132) the function recurses into EnhancedMaskTest(mask + 1, name) (line 134). On failure it advances name by one and loops (line 151), so the next iteration tries the rest of the mask at every later name position. There is no memoization and no greedy last-star shortcut, so k stars cost O(n^k).

Suggested fix

Use the standard iterative glob algorithm that remembers only the last * position and the matching name position (two saved pointers, no recursion). It is O(n*m) and gives the same results for * and ?. Case-folding with MyCharUpper stays as is.

Related

Not related to the known items (// path handling, rn, u -uq3, 7z coder IDs).


Found with a differential test setup (a model of the documented behavior, and for some reports an independent tool such as libarchive/bsdtar, compared against the real binary) and then checked by hand: every command above was re-run verbatim on the tested build, and the root cause was read in the source. Tested commit: d3296e3 (26.06).

Lingua principale
C++
Stelle
4.6k
Fork
519
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ip7z/7zip

Tutte le issue di ip7z/7zip

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.