From d6d5951b957c18d60998d6d000cf36b7a1d57cd5 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 2 Nov 2024 23:15:15 +0000 Subject: [PATCH] Fix allocation issue in FSA module --- lib/fsa.c | 6 ++++-- tests/integration_tests.c | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/fsa.c b/lib/fsa.c index 092819a..7e5f7a3 100644 --- a/lib/fsa.c +++ b/lib/fsa.c @@ -33,7 +33,8 @@ int fsa_add_state(fsa_t *fsa) { if (fsa->count >= fsa->capacity) { fsa->capacity *= 2; - fsa->states = realloc(fsa->states, fsa->capacity); + fsa->states + = realloc(fsa->states, fsa->capacity * sizeof(fsa_state_t)); assert(NULL != fsa->states); } @@ -56,7 +57,8 @@ void fsa_add_rule(fsa_t *fsa, int from, int to, int input) fsa_state_t *state = &fsa->states[from]; if (state->count >= state->capacity) { state->capacity *= 2; - state->rules = realloc(state->rules, state->capacity); + state->rules + = realloc(state->rules, state->capacity * sizeof(fsa_rule_t)); assert(NULL != state->rules); } diff --git a/tests/integration_tests.c b/tests/integration_tests.c index 5e95090..da2ea4b 100644 --- a/tests/integration_tests.c +++ b/tests/integration_tests.c @@ -63,6 +63,17 @@ static void test_arbitrary_regex_2(void) fsa_free(&dfa); } +static void test_system_header_include_regex(void) +{ + fsa_t dfa; + const char *regex = "#include <[abcdefghijklmnopqrstuvwxyz]+\\.h>"; + const bool success = compile(regex, strlen(regex), &dfa); + ASSERT_TRUE(success); + ASSERT_ACCEPTS(&dfa, "#include "); + ASSERT_REJECTS(&dfa, "#include \"foo.h\""); + fsa_free(&dfa); +} + int main(void) { TESTING_BEGIN(); @@ -70,5 +81,6 @@ int main(void) test_even_number_of_Is_regex(); test_arbitrary_regex_1(); test_arbitrary_regex_2(); + test_system_header_include_regex(); return TESTING_END(); }