Skip to content
Myr

dmenu: fix --index desync when an input entry is dropped

Before adoption: this commit came in with the history the project adopted, so no landing records it.

dmenu_load_entries() skips any stdin entry that fails ambstoc32() (e.g. invalid multibyte data) via a bare `continue`, without advancing app_idx. Since app_idx becomes the entry's app->index -- the value --dmenu --index later prints for whatever gets selected -- every entry after a dropped one ends up with an index that is too low by the number of entries dropped before it. This is easy to trigger in practice: any upstream producer that truncates preview text to a fixed byte length (cliphist does this) can slice a multi-byte UTF-8 character in half, producing one invalid entry partway down the list. From that point on, clicking or otherwise selecting an entry and asking fuzzel to --index it back resolves to the wrong line -- fuzzel's own selection state is correct (confirmed by instrumenting execute_selected() and dmenu_execute() directly: both agree on the intended entry's title), but the index handed back to the caller points one row short of it, so the caller ends up acting on an unrelated entry instead. Fix: advance app_idx on the dropped-entry path too, so the counter always reflects each entry's true ordinal position in the original input, independent of whether that particular entry made it into the visible list. Verified with `--auto-select --search=...` against a captured real-world input containing one entry with a truncated (invalid) UTF-8 preview: index printed for the entry two rows below it went from 40 (wrong; the deleted entry's own would-be index) to 41 (correct, matching its actual 0-based stdin line number).

authored by Stephen Talley · committed by Daniel Eklöf · parent 7f0dc9d · Browse files · 2 files+20 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+20 added−0 removed

  • other:+20 added−0 removed

2 files+20 added−0 removed

modified:CHANGELOG.md+7 added
38 unmodified lines
39### Changed40### Deprecated41### Removed42### Fixed4344* dmenu mode: `--index` returning an index one (or more) lower than45 the selected entry's actual position in the input whenever an46 earlier entry in the input was dropped for being unusable (e.g.47 invalid multibyte data), silently executing the wrong entry.484950### Security51### Contributors5253
1194 unmodified lines
modified:dmenu.c+13 added
161 unmodified lines
162163 memmove(buffer, delim_at + 1, size - consume);164 size -= consume;165166 /*167 * Every delimited chunk of stdin -- valid or not -- occupies one168 * ordinal position in the original input, and --index promises169 * to return that original position. The counter must therefore170 * advance here even when the entry itself is dropped for being171 * unusable (e.g. invalid multibyte data, such as an upstream172 * producer truncating a line mid UTF-8 sequence). Skipping the173 * increment on this path silently shifts the index of every174 * subsequent entry by one for each dropped entry, so an175 * unrelated later item gets selected/executed instead of the176 * one the user actually picked.177 */178 if (wline == NULL) {179 free(icon_name);180 app_idx++;181 continue;182 }183184 char32_t *title = with_nth_format == NULL
162 unmodified lines