commit 2619a749a5329fd24d10286ecd16f59791436fb0
parent 6f0ef8a5f2efc996dc8f79c0bf0ed36dac7e1ac7
Author: zeertzjq <zeertzjq@outlook.com>
Date: Thu, 14 Aug 2025 06:45:30 +0800
vim-patch:9.1.1628: fuzzy.c has a few issues
Problem: fuzzy.c has a few issues
Solution: Use Vims memory management, update style
(glepnir)
Problem:
- Missing cleanup of lmatchpos lists causing memory leaks
- Missing error handling for list operations
- Use of malloc() instead of Vim's alloc() functions
- Inconsistent C-style comments
- Missing null pointer checks for memory allocation
- Incorrect use of vim_free() for list objects
Solution:
- Add proper cleanup of lmatchpos in done section using list_free()
- Set lmatchpos to NULL after successful transfer to avoid confusion
- Add error handling for list_append_tv() failures
- Replace malloc() with alloc() and add null pointer checks
- Convert C-style comments to C++ style for consistency
- Fix vim_free() calls to use list_free() for list objects
closes: vim/vim#17984
https://github.com/vim/vim/commit/17a6d696bd2f9526f2c4beae95208626d0e8d922
Co-authored-by: glepnir <glephunter@gmail.com>
Diffstat:
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/src/nvim/fuzzy.c b/src/nvim/fuzzy.c
@@ -50,7 +50,7 @@
typedef double score_t;
#define SCORE_MAX INFINITY
-#define SCORE_MIN -INFINITY
+#define SCORE_MIN (-INFINITY)
#define SCORE_SCALE 1000
typedef struct {
@@ -253,8 +253,9 @@ static void fuzzy_match_in_list(list_T *const l, char *const str, const bool mat
items[match_count].itemstr = itemstr_allocate ? xstrdup(itemstr) : itemstr;
items[match_count].itemstr_allocated = itemstr_allocate;
- // Copy the list of matching positions in itemstr to a list.
- {
+ // Copy the list of matching positions in itemstr to a list, if
+ // "retmatchpos" is set.
+ if (retmatchpos) {
items[match_count].lmatchpos = tv_list_alloc(kListLenMayKnow);
int j = 0;
const char *p = str;
@@ -305,6 +306,7 @@ static void fuzzy_match_in_list(list_T *const l, char *const str, const bool mat
for (int i = 0; i < match_count; i++) {
assert(items[i].lmatchpos != NULL);
tv_list_append_list(retlist, items[i].lmatchpos);
+ items[i].lmatchpos = NULL;
}
// copy the matching scores
@@ -321,6 +323,7 @@ static void fuzzy_match_in_list(list_T *const l, char *const str, const bool mat
if (items[i].itemstr_allocated) {
xfree(items[i].itemstr);
}
+ assert(items[i].lmatchpos == NULL);
}
xfree(items);
}
@@ -901,9 +904,8 @@ static score_t match_positions(const char *const needle, const char *const hayst
// If this score was determined using
// SCORE_MATCH_CONSECUTIVE, the
// previous character MUST be a match
- match_required =
- i && j
- && M[i][j] == D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE;
+ match_required = i && j
+ && M[i][j] == D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE;
positions[i] = (uint32_t)(j--);
break;
}