From 97de6cda7667c1aba8a5574bd2d4aee5e604918d Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 14 Apr 2026 12:45:33 +0300 Subject: [PATCH] all: For huge v files, array_ensure_cap called by the checker seems to go out of bounds (fixes #26141) --- vlib/v/util/suggestions.v | 4 ++++ vlib/v/util/suggestions_test.v | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 vlib/v/util/suggestions_test.v diff --git a/vlib/v/util/suggestions.v b/vlib/v/util/suggestions.v index f8d644e15..b17e92757 100644 --- a/vlib/v/util/suggestions.v +++ b/vlib/v/util/suggestions.v @@ -38,6 +38,7 @@ pub mut: // new_suggestion creates a new Suggestion, given a wanted value and a list of possibilities. pub fn new_suggestion(wanted string, possibilities []string, params SuggestionParams) Suggestion { mut s := Suggestion{ + known: []Possibility{cap: int(max_suggestions_limit)} wanted: wanted swanted: short_module_name(wanted) similarity_threshold: params.similarity_threshold @@ -75,6 +76,9 @@ pub fn (mut s Suggestion) add(val string) { // add adds all of the `many` to the list of known possibilities of the suggestion pub fn (mut s Suggestion) add_many(many []string) { for x in many { + if s.known.len >= max_suggestions_limit { + break + } s.add(x) } } diff --git a/vlib/v/util/suggestions_test.v b/vlib/v/util/suggestions_test.v new file mode 100644 index 000000000..f16562c85 --- /dev/null +++ b/vlib/v/util/suggestions_test.v @@ -0,0 +1,21 @@ +module util + +fn test_new_suggestion_preallocates_the_bounded_storage() { + s := new_suggestion('missing_name', []string{}, SuggestionParams{}) + assert s.known.len == 0 + assert s.known.cap == max_suggestions_limit +} + +fn test_new_suggestion_caps_the_number_of_known_possibilities() { + mut possibilities := []string{cap: int(max_suggestions_limit) + 50} + for i in 0 .. int(max_suggestions_limit) + 50 { + possibilities << 'candidate_${i}' + } + s := new_suggestion('missing_name', possibilities, SuggestionParams{}) + assert s.known.len == max_suggestions_limit + assert s.known.cap == max_suggestions_limit + known_values := s.known.map(it.value) + assert 'candidate_0' in known_values + assert 'candidate_${max_suggestions_limit - 1}' in known_values + assert 'candidate_${max_suggestions_limit}' !in known_values +} -- 2.39.5