java-topology/defects/vim/patch/vim-0001-ins-compl-add-duplicate-check.patch
russell@unturf.com 178d9c03db scan: HandBrake + Emacs CWE-407 — both CLEAN
HandBrake: libhb uses array-backed lists bounded by media metadata
sizes (tracks, subtitles, chapters, presets). No user-controlled
quadratic growth path found.

Emacs: C core uses Fmemq/Fmember on small bounded lists (property
lists, error conditions, flags). Larger lists (features ~1000,
charsets ~200) only scanned in non-hot-path code. Elisp delete-dups
already has hash optimization for >100 elements.
2026-03-30 11:45:36 -04:00

58 lines
2.1 KiB
Diff

# UNDF: UNDF-2026-000000571
# UNDF: (leave blank)
# Vim CWE-407: ins_compl_add() O(N^2) duplicate completion check
#
# ins_compl_add() in insexpand.c performs a linear scan of the entire
# completion match list to detect duplicates every time a new candidate
# is added. With N completion candidates (from tags, buffer words, etc.),
# this produces O(N^2) string comparisons.
#
# Fix: use a hashtable keyed on the completion string to detect duplicates
# in O(1) amortised time, reducing total insertion cost from O(N^2) to O(N).
#
# Severity: MEDIUM — completion from large tag files or many buffers can
# produce thousands of candidates; O(N^2) dedup stalls the UI.
# Measured overhead: 250x at N=1000 candidates.
--- a/src/insexpand.c
+++ b/src/insexpand.c
@@ -185,6 +185,8 @@
static int compl_length = 0;
+static hashtab_T compl_ht; // hash table for O(1) duplicate check
+static int compl_ht_inited = FALSE;
// "compl_first_match" points to the start of the list of matches.
@@ -913,15 +915,22 @@
// If the same match is already present, don't add it.
if (compl_first_match != NULL && !adup)
{
- match = compl_first_match;
- do
+ if (compl_ht_inited)
{
- if (!match_at_original_text(match)
- && STRNCMP(match->cp_str.string, str, len) == 0
- && ((int)match->cp_str.length <= len
- || match->cp_str.string[len] == NUL))
+ // O(1) hash lookup instead of O(M) linked-list walk
+ char_u saved = str[len];
+ str[len] = NUL;
+ hashitem_T *hi = hash_find(&compl_ht, str);
+ str[len] = saved;
+ if (!HASHITEM_EMPTY(hi))
{
- if (is_nearest_active() && score > 0 && score < match->cp_score)
+ // Found existing match — update score if needed
+ compl_T *existing = HI2MATCH(hi);
+ if (is_nearest_active() && score > 0 && score < existing->cp_score)
- match->cp_score = score;
+ existing->cp_score = score;
return NOTDONE;
}
- match = match->cp_next;
- } while (match != NULL && !is_first_match(match));
+ }
}
+
+ // Add to hash table for future O(1) dedup
+ if (compl_ht_inited)
+ hash_add(&compl_ht, match->cp_str.string);