fix: memory leak in get_positions when using or (#59)

This commit is contained in:
Simon Hauser
2022-02-18 21:36:29 +01:00
committed by GitHub
parent 1b5d0cba81
commit f917416050
4 changed files with 18 additions and 10 deletions

View File

@@ -3,4 +3,4 @@ line_endings = "Unix"
indent_type = "Spaces"
indent_width = 2
quote_style = "AutoPreferDouble"
no_call_parentheses = true
call_parentheses = "None"

View File

@@ -1,5 +1,4 @@
CFLAGS = -Wall -Werror -fpic -std=gnu99
COVERAGE ?=
ifeq ($(OS),Windows_NT)
MKD = -mkdir
@@ -19,7 +18,7 @@ build/$(TARGET): src/fzf.c src/fzf.h
$(CC) -O3 $(CFLAGS) -shared src/fzf.c -o build/$(TARGET)
build/test: build/$(TARGET) test/test.c
$(CC) -Og -ggdb3 $(CFLAGS) $(COVERAGE) test/test.c -o build/test -I./src -L./build -lfzf -lexaminer
$(CC) -Og -ggdb3 $(CFLAGS) test/test.c -o build/test -I./src -L./build -lfzf -lexaminer
build/benchmark: build/$(TARGET) test/benchmark.c
$(CC) -O3 $(CFLAGS) test/benchmark.c -o build/benchmark -I./src -L./build -lfzf -lcurl -lm
@@ -31,10 +30,6 @@ lint:
format:
clang-format --style=file --dry-run -Werror src/fzf.c src/fzf.h test/test.c test/benchmark.c
debug:
$(MKD) build
$(CC) -Og -ggdb3 $(CFLAGS) $(COVERAGE) -shared src/fzf.c -o build/$(TARGET)
test: build/test
@LD_LIBRARY_PATH=${PWD}/build:${LD_LIBRARY_PATH} ./build/test

View File

@@ -199,7 +199,7 @@ static size_t min64u(size_t a, size_t b) {
}
static fzf_position_t *pos_array(bool with_pos, size_t len) {
if (with_pos) {
if (with_pos && len > 0) {
fzf_position_t *pos = (fzf_position_t *)malloc(sizeof(fzf_position_t));
pos->size = 0;
pos->cap = len;
@@ -1232,7 +1232,10 @@ int32_t fzf_get_score(const char *text, fzf_pattern_t *pattern,
}
current_score = res.score;
matched = true;
} else if (term->inv) {
break;
}
if (term->inv) {
current_score = 0;
matched = true;
}
@@ -1274,7 +1277,10 @@ fzf_position_t *fzf_get_positions(const char *text, fzf_pattern_t *pattern,
}
current_res = res;
matched = true;
} else if (term->inv) {
break;
}
if (term->inv) {
matched = true;
}
}

View File

@@ -738,6 +738,13 @@ TEST(pos_integration, simple_or) {
pos_wrapper("'src | ^Lua", input, expected);
}
TEST(pos_integration, or_mem_leak) {
char *input[] = {"src/fzf.h", NULL};
int match1[] = {2, 1, 0, -1};
int *expected[] = {match1};
pos_wrapper("src | src", input, expected);
}
TEST(pos_integration, complex_term) {
char *input[] = {"lua/random_previewer", "README.md",
"previewers/utils.lua", "previewers/buffer.lua",