From 01fb9be1e71b2c25cee0ca914767bb2d92ac2079 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Fri, 25 Oct 2024 18:46:01 +0100 Subject: [PATCH] Initialize everything explicitly in parser --- lib/parser.c | 10 ++++++++-- tests/parser_tests.c | 32 ++++++++++++++++---------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/parser.c b/lib/parser.c index 3a5acc9..2c4d5a4 100644 --- a/lib/parser.c +++ b/lib/parser.c @@ -55,6 +55,8 @@ static int parse_class(const char *input, int rem, class_t *out) if (used < rem && '^' == input[used]) { out->negated = true; ++used; + } else { + out->negated = false; } out->count = 0; @@ -139,6 +141,8 @@ static int parse_term(const char *input, int rem, term_t *out) default: out->quantifier = QUANTIFIER_NONE; } + } else { + out->quantifier = QUANTIFIER_NONE; } return used; @@ -150,7 +154,7 @@ static int parse_sequence(const char *input, int rem, sequence_t *out) out->len = 0; out->capacity = SEQUENCE_START_CAPACITY; - out->contents = calloc(out->capacity, sizeof(term_t)); + out->contents = malloc(out->capacity * sizeof(term_t)); if (NULL == out->contents) return -1; @@ -186,13 +190,15 @@ int parse_regex(const char *input, int rem, regex_t *out) if (used < rem && '|' == input[used]) { ++used; - out->alternative = calloc(1, sizeof(regex_t)); + out->alternative = malloc(sizeof(regex_t)); if (NULL == out->alternative) return -1; result = parse_regex(input + used, rem - used, out->alternative); if (result < 0) return -1; used += result; + } else { + out->alternative = NULL; } return used; diff --git a/tests/parser_tests.c b/tests/parser_tests.c index 027f6b5..3165eae 100644 --- a/tests/parser_tests.c +++ b/tests/parser_tests.c @@ -10,7 +10,7 @@ static void a_has_no_alternative(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING("a", &r); ASSERT_NE(-1, result); ASSERT_NULL(r.alternative); @@ -19,7 +19,7 @@ static void a_has_no_alternative(void) static void a_pipe_b_has_alternative(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING("a|b", &r); ASSERT_NE(-1, result); ASSERT_NOT_NULL(r.alternative); @@ -28,7 +28,7 @@ static void a_pipe_b_has_alternative(void) static void a_pipe_b_pipe_c_result_alternative_has_alternative(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING("a|b|c", &r); ASSERT_NE(-1, result); ASSERT_NOT_NULL(r.alternative); @@ -38,7 +38,7 @@ static void a_pipe_b_pipe_c_result_alternative_has_alternative(void) static void a_is_parsed_as_unquantified_literal(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING("a", &r); ASSERT_NE(-1, result); @@ -52,7 +52,7 @@ static void a_is_parsed_as_unquantified_literal(void) static void b_is_parsed_as_unquantified_literal(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING("b", &r); ASSERT_NE(-1, result); @@ -66,7 +66,7 @@ static void b_is_parsed_as_unquantified_literal(void) static void abc_is_parsed_as_sequence_of_unquantified_literals(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING("abc", &r); ASSERT_NE(-1, result); @@ -86,7 +86,7 @@ static void abc_is_parsed_as_sequence_of_unquantified_literals(void) static void dot_is_parsed_as_unquantified_wildcard_term(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING(".", &r); ASSERT_NE(-1, result); @@ -99,7 +99,7 @@ static void dot_is_parsed_as_unquantified_wildcard_term(void) static void backslash_dot_is_parsed_as_unquantified_literal(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING("\\.", &r); ASSERT_NE(-1, result); @@ -113,7 +113,7 @@ static void backslash_dot_is_parsed_as_unquantified_literal(void) static void backslash_backslash_is_parsed_as_unquantified_literal(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING("\\\\", &r); ASSERT_NE(-1, result); @@ -127,7 +127,7 @@ static void backslash_backslash_is_parsed_as_unquantified_literal(void) static void a_pipe_b_in_parens_is_parsed_as_regex_term(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING("(a|b)", &r); ASSERT_NE(-1, result); @@ -152,7 +152,7 @@ static void a_pipe_b_in_parens_is_parsed_as_regex_term(void) static void a_in_parens_b_is_parsed_as_sequence_with_regex_term(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING("(a)b", &r); ASSERT_NE(-1, result); @@ -174,7 +174,7 @@ static void a_in_parens_b_is_parsed_as_sequence_with_regex_term(void) static void dot_star_is_parsed_as_zero_or_more_wildcard(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING(".*", &r); ASSERT_NE(-1, result); @@ -187,7 +187,7 @@ static void dot_star_is_parsed_as_zero_or_more_wildcard(void) static void dot_plus_is_parsed_as_one_or_more_wildcard(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING(".+", &r); ASSERT_NE(-1, result); @@ -200,7 +200,7 @@ static void dot_plus_is_parsed_as_one_or_more_wildcard(void) static void dot_question_mark_is_parsed_as_zero_or_one_wildcard(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING(".?", &r); ASSERT_NE(-1, result); @@ -213,7 +213,7 @@ static void dot_question_mark_is_parsed_as_zero_or_one_wildcard(void) static void a_in_brackets_is_parsed_as_class_containing_only_a(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING("[a]", &r); ASSERT_NE(-1, result); @@ -230,7 +230,7 @@ static void a_in_brackets_is_parsed_as_class_containing_only_a(void) static void caret_a_in_brackets_parses_as_negated_class(void) { - regex_t r = { 0 }; + regex_t r; const int result = PARSE_REGEX_STRING("[^a]", &r); ASSERT_NE(-1, result);