summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Kaster <andrewdkaster@gmail.com>2021-05-12 04:48:03 -0600
committerLinus Groh <mail@linusgroh.de>2021-05-14 08:34:00 +0100
commit55d338b66f3c3404baa38d3954f63b38ee813af5 (patch)
treefc0e6853519d6bfb537565a9c4c7134647aa7597
parent09fe9f4542bdada7369a19f5c3fc3b9c0b21969f (diff)
downloadserenity-55d338b66f3c3404baa38d3954f63b38ee813af5.zip
Tests: Free all memory allocated with regcomp in RegexLibC tests
The C interface (posix interface?) for regexes has no "initialize" function, only a free function. The comment in regcomp in LibRegex/C/Regex.cpp notes that calling regcomp without a regfree is an error, and will leak memory. Every single time regcomp is called on a regex_t*, it will allocate new memory. Make sure that all the regcomp calls are paired with a regfree in the tests program
-rw-r--r--Tests/LibRegex/RegexLibC.cpp17
1 files changed, 14 insertions, 3 deletions
diff --git a/Tests/LibRegex/RegexLibC.cpp b/Tests/LibRegex/RegexLibC.cpp
index bea5e5984c..3df9837031 100644
--- a/Tests/LibRegex/RegexLibC.cpp
+++ b/Tests/LibRegex/RegexLibC.cpp
@@ -327,6 +327,7 @@ TEST_CASE(parser_error_special_characters_used_at_wrong_place)
pattern = b.build();
EXPECT_EQ(regcomp(&regex, pattern.characters(), REG_EXTENDED), error_code_to_check);
EXPECT_EQ(regexec(&regex, "test", num_matches, matches, 0), error_code_to_check);
+ regfree(&regex);
// After vertical line
b.clear();
@@ -335,6 +336,7 @@ TEST_CASE(parser_error_special_characters_used_at_wrong_place)
pattern = b.build();
EXPECT_EQ(regcomp(&regex, pattern.characters(), REG_EXTENDED), error_code_to_check);
EXPECT_EQ(regexec(&regex, "test", num_matches, matches, 0), error_code_to_check);
+ regfree(&regex);
// After circumflex
b.clear();
@@ -343,6 +345,7 @@ TEST_CASE(parser_error_special_characters_used_at_wrong_place)
pattern = b.build();
EXPECT_EQ(regcomp(&regex, pattern.characters(), REG_EXTENDED), error_code_to_check);
EXPECT_EQ(regexec(&regex, "test", num_matches, matches, 0), error_code_to_check);
+ regfree(&regex);
// After dollar
b.clear();
@@ -351,6 +354,7 @@ TEST_CASE(parser_error_special_characters_used_at_wrong_place)
pattern = b.build();
EXPECT_EQ(regcomp(&regex, pattern.characters(), REG_EXTENDED), error_code_to_check);
EXPECT_EQ(regexec(&regex, "test", num_matches, matches, 0), error_code_to_check);
+ regfree(&regex);
// After left parens
b.clear();
@@ -360,9 +364,8 @@ TEST_CASE(parser_error_special_characters_used_at_wrong_place)
pattern = b.build();
EXPECT_EQ(regcomp(&regex, pattern.characters(), REG_EXTENDED), error_code_to_check);
EXPECT_EQ(regexec(&regex, "test", num_matches, matches, 0), error_code_to_check);
+ regfree(&regex);
}
-
- regfree(&regex);
}
TEST_CASE(parser_error_vertical_line_used_at_wrong_place)
@@ -376,22 +379,24 @@ TEST_CASE(parser_error_vertical_line_used_at_wrong_place)
pattern = "|asdf";
EXPECT_EQ(regcomp(&regex, pattern.characters(), REG_EXTENDED), REG_EMPTY_EXPR);
EXPECT_EQ(regexec(&regex, "test", num_matches, matches, 0), REG_EMPTY_EXPR);
+ regfree(&regex);
// Last in ere
pattern = "asdf|";
EXPECT_EQ(regcomp(&regex, pattern.characters(), REG_EXTENDED), REG_EMPTY_EXPR);
EXPECT_EQ(regexec(&regex, "test", num_matches, matches, 0), REG_EMPTY_EXPR);
+ regfree(&regex);
// After left parens
pattern = "(|asdf)";
EXPECT_EQ(regcomp(&regex, pattern.characters(), REG_EXTENDED), REG_EMPTY_EXPR);
EXPECT_EQ(regexec(&regex, "test", num_matches, matches, 0), REG_EMPTY_EXPR);
+ regfree(&regex);
// Proceed right parens
pattern = "(asdf)|";
EXPECT_EQ(regcomp(&regex, pattern.characters(), REG_EXTENDED), REG_EMPTY_EXPR);
EXPECT_EQ(regexec(&regex, "test", num_matches, matches, 0), REG_EMPTY_EXPR);
-
regfree(&regex);
}
@@ -969,6 +974,7 @@ TEST_CASE(simple_bracket_chars)
EXPECT_EQ(regexec(&regex, "c", 0, NULL, 0), REG_NOERR);
EXPECT_EQ(regexec(&regex, "d", 0, NULL, 0), REG_NOMATCH);
EXPECT_EQ(regexec(&regex, "e", 0, NULL, 0), REG_NOMATCH);
+ regfree(&regex);
}
TEST_CASE(simple_bracket_chars_inverse)
@@ -982,6 +988,7 @@ TEST_CASE(simple_bracket_chars_inverse)
EXPECT_EQ(regexec(&regex, "c", 0, NULL, 0), REG_NOMATCH);
EXPECT_EQ(regexec(&regex, "d", 0, NULL, 0), REG_NOERR);
EXPECT_EQ(regexec(&regex, "e", 0, NULL, 0), REG_NOERR);
+ regfree(&regex);
}
TEST_CASE(simple_bracket_chars_range)
@@ -995,6 +1002,7 @@ TEST_CASE(simple_bracket_chars_range)
EXPECT_EQ(regexec(&regex, "c", 0, NULL, 0), REG_NOERR);
EXPECT_EQ(regexec(&regex, "d", 0, NULL, 0), REG_NOERR);
EXPECT_EQ(regexec(&regex, "e", 0, NULL, 0), REG_NOMATCH);
+ regfree(&regex);
}
TEST_CASE(simple_bracket_chars_range_inverse)
@@ -1010,6 +1018,7 @@ TEST_CASE(simple_bracket_chars_range_inverse)
EXPECT_EQ(regexec(&regex, "e", 0, NULL, 0), REG_NOERR);
EXPECT_EQ(regexec(&regex, "k", 0, NULL, 0), REG_NOMATCH);
EXPECT_EQ(regexec(&regex, "z", 0, NULL, 0), REG_NOMATCH);
+ regfree(&regex);
}
TEST_CASE(bracket_character_class_uuid)
@@ -1035,6 +1044,7 @@ TEST_CASE(simple_bracket_character_class_inverse)
EXPECT_EQ(regexec(&regex, "3", 0, NULL, 0), REG_NOMATCH);
EXPECT_EQ(regexec(&regex, "d", 0, NULL, 0), REG_NOERR);
EXPECT_EQ(regexec(&regex, "e", 0, NULL, 0), REG_NOERR);
+ regfree(&regex);
}
TEST_CASE(email_address)
@@ -1115,4 +1125,5 @@ TEST_CASE(simple_notbol_noteol)
EXPECT_EQ(regexec(&regex2, "hello friends", 0, NULL, REG_NOTEOL), REG_NOMATCH);
regfree(&regex);
+ regfree(&regex2);
}