[PATCH 5/9] test-tool regex: call regfree(), fix memory leaks

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Fix memory leaks in "test-tool regex" which have been there since
c91841594c2 (test-regex: Add a test to check for a bug in the regex
routines, 2012-09-01), as a result we can mark a test as passing with
SANITIZE=leak using "TEST_PASSES_SANITIZE_LEAK=true".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@xxxxxxxxx>
---
 t/helper/test-regex.c           | 40 ++++++++++++++++++++-------------
 t/t7812-grep-icase-non-ascii.sh |  1 +
 2 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/t/helper/test-regex.c b/t/helper/test-regex.c
index d6f28ca8d14..a37d1f7a546 100644
--- a/t/helper/test-regex.c
+++ b/t/helper/test-regex.c
@@ -24,27 +24,35 @@ static int test_regex_bug(void)
 	char *str = "={}\nfred";
 	regex_t r;
 	regmatch_t m[1];
+	int err = 0;
 
 	if (regcomp(&r, pat, REG_EXTENDED | REG_NEWLINE))
 		die("failed regcomp() for pattern '%s'", pat);
-	if (regexec(&r, str, 1, m, 0))
-		die("no match of pattern '%s' to string '%s'", pat, str);
+	if (regexec(&r, str, 1, m, 0)) {
+		err = error("no match of pattern '%s' to string '%s'", pat, str);
+		goto cleanup;
+	}
 
 	/* http://sourceware.org/bugzilla/show_bug.cgi?id=3957  */
-	if (m[0].rm_so == 3) /* matches '\n' when it should not */
-		die("regex bug confirmed: re-build git with NO_REGEX=1");
+	if (m[0].rm_so == 3) { /* matches '\n' when it should not */
+		err = error("regex bug confirmed: re-build git with NO_REGEX=1");
+		goto cleanup;
+	}
 
-	return 0;
+cleanup:
+	regfree(&r);
+	return err < 0 ? 1 : 0;
 }
 
 int cmd__regex(int argc, const char **argv)
 {
 	const char *pat;
 	const char *str;
-	int ret, silent = 0, flags = 0;
+	int silent = 0, flags = 0;
 	regex_t r;
 	regmatch_t m[1];
 	char errbuf[64];
+	int ret = 0;
 
 	argv++;
 	argc--;
@@ -85,27 +93,29 @@ int cmd__regex(int argc, const char **argv)
 	}
 	git_setup_gettext();
 
-	ret = regcomp(&r, pat, flags);
-	if (ret) {
+	if (regcomp(&r, pat, flags)) {
 		if (silent)
-			return ret;
+			return 1;
 
 		regerror(ret, &r, errbuf, sizeof(errbuf));
 		die("failed regcomp() for pattern '%s' (%s)", pat, errbuf);
 	}
 	if (!str)
-		return 0;
+		goto cleanup;
 
-	ret = regexec(&r, str, 1, m, 0);
-	if (ret) {
+	if (regexec(&r, str, 1, m, 0)) {
+		ret = 1;
 		if (silent || ret == REG_NOMATCH)
-			return ret;
+			goto cleanup;
 
 		regerror(ret, &r, errbuf, sizeof(errbuf));
-		die("failed regexec() for subject '%s' (%s)", str, errbuf);
+		error("failed regexec() for subject '%s' (%s)", str, errbuf);
+		goto cleanup;
 	}
 
-	return 0;
+cleanup:
+	regfree(&r);
+	return ret;
 usage:
 	usage("\ttest-tool regex --bug\n"
 	      "\ttest-tool regex [--silent] <pattern>\n"
diff --git a/t/t7812-grep-icase-non-ascii.sh b/t/t7812-grep-icase-non-ascii.sh
index ac7be547145..31c66b63c2c 100755
--- a/t/t7812-grep-icase-non-ascii.sh
+++ b/t/t7812-grep-icase-non-ascii.sh
@@ -2,6 +2,7 @@
 
 test_description='grep icase on non-English locales'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./lib-gettext.sh
 
 doalarm () {
-- 
2.37.0.874.g7d3439f13c4




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux