Skip FZF_CURRENT_ITEM export for items larger than 64 KB
Before adoption: this commit came in with the history the project adopted, so no landing records it.
A huge item can overflow ARG_MAX and break exec with E2BIG, failing preview and other child commands. (#4806)
authored by Junegunn Choi · committed by Junegunn Choi · parent 1090578 · Browse files · 4 files·+32 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+32 added−2 removed
- tests:+20 added−0 removed
- other:+12 added−2 removed
Provenance
- Commit
f2e4515- Tree
9ce2fca- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
1090578
4 files changed+32 added−2 removed
modified:man/man1/fzf.1+3 added
1532 unmodified lines1533 1534 .PP1535 .B FZF_CURRENT_ITEM1536 is omitted when the item contains a NUL byte, because exec(2) cannot pass it.1537+It is also omitted when the item is larger than 64 KB, so that a huge item1538+cannot overflow the environment size limit and break preview and other child1539+commands.1540 1541 .SH EXTENDED SEARCH MODE1542 1543 Unless specified otherwise, fzf will start in "extended\-search mode". In this811 unmodified linesmodified:src/terminal.go+8 added-2 removed
66 unmodified lines67 // execute-silent and transform* actions will block user input for this duration.68 // After this duration, users can press CTRL-C to terminate the command.69 const blockDuration = 1 * time.Second70 71+// Skip exporting FZF_CURRENT_ITEM when the item is larger than this, so a huge72+// item cannot overflow ARG_MAX and break exec for preview and other commands.73+const maxCurrentItemEnvSize = 64 * 102474+75 func init() {76 placeholder = regexp.MustCompile(`\\?(?:{[+*sfr]*[0-9,-.]*}|{q(?::s?[0-9,-.]+)?}|{fzf:(?:query|action|prompt)}|{[+*]?f?nf?})`)77 whiteSuffix = regexp.MustCompile(`\s*$`)78 offsetComponentRegex = regexp.MustCompile(`([+-][0-9]+)|(-?/[1-9][0-9]*)`)1368 unmodified lines1447 env = append(env, fmt.Sprintf("FZF_LINES=%d", t.areaLines))1448 env = append(env, fmt.Sprintf("FZF_COLUMNS=%d", t.areaColumns))1449 env = append(env, fmt.Sprintf("FZF_POS=%d", min(t.merger.Length(), t.cy+1)))1450 if item := t.currentItem(); item != nil {1447- // Skip if the value contains a NUL byte; exec(2) would reject the env.1448- if s := item.AsString(t.ansi); !strings.ContainsRune(s, 0) {1451+ // Skip if the value contains a NUL byte (exec(2) would reject the env)1452+ // or is too large (a huge item can overflow ARG_MAX and break exec1453+ // entirely for preview and other child commands).1454+ if s := item.AsString(t.ansi); !strings.ContainsRune(s, 0) && len(s) <= maxCurrentItemEnvSize {1455 env = append(env, "FZF_CURRENT_ITEM="+s)1456 }1457 }1458 env = append(env, fmt.Sprintf("FZF_CLICK_HEADER_LINE=%d", t.clickHeaderLine))6929 unmodified linesmodified:test/test_core.rb+20 added
2344 unmodified lines2345 assert_equal expected, env_vars.slice(*expected.keys)2346 end2347 end2348 2349+ def test_env_current_item_size_limit2350+ preview = %[(echo START; env | grep '^FZF_CURRENT_ITEM='; echo END) > #{tempname}]2351+ # Large item (> 64 KB) is omitted so it cannot overflow ARG_MAX and break exec2352+ tmux.send_keys %(head -c 70000 /dev/zero | tr '\\0' a | #{FZF} --preview-window 0 --preview "#{preview}"), :Enter2353+ wait do2354+ content = File.exist?(tempname) ? File.read(tempname) : ''2355+ assert_includes content, 'END'2356+ refute_includes content, 'FZF_CURRENT_ITEM='2357+ end2358+ tmux.send_keys :Enter2359+ FileUtils.rm_f(tempname)2360+ # Smaller item is exported as usual2361+ tmux.send_keys %(head -c 1000 /dev/zero | tr '\\0' a | #{FZF} --preview-window 0 --preview "#{preview}"), :Enter2362+ wait do2363+ content = File.exist?(tempname) ? File.read(tempname) : ''2364+ assert_includes content, 'END'2365+ assert_includes content, 'FZF_CURRENT_ITEM=' + ('a' * 1000)2366+ end2367+ end2368+2369 def test_abort_action_chain2370 tmux.send_keys %(seq 100 | #{FZF} --bind 'load:accept+up+up' > #{tempname}), :Enter2371 wait do2372 assert_path_exists tempname461 unmodified linesmodified:CHANGELOG.md+1 added
10 unmodified lines11 (seq 100; sleep 1; seq 100) | fzf --query 1 \12 --bind 'result:transform-header(echo result: $FZF_MATCH_COUNT),result-final:transform-footer(echo final: $FZF_MATCH_COUNT)'13 ```14 - Bound `alt-left` to `backward-word` and `alt-right` to `forward-word` by default (#4833)15+- Skip `$FZF_CURRENT_ITEM` export when the item is larger than 64 KB; a huge item can overflow `ARG_MAX` and break preview and other child commands with `E2BIG` (#4806)16 17 0.73.118 ------19 - Bug fixes3315 unmodified lines