Thread (1 message) 1 message, 1 author, 2020-05-15

Re: [PATCH v2] t4210: detect REG_ILLSEQ dynamically

From: Junio C Hamano <hidden>
Date: 2020-05-15 21:48:30
Subsystem: the rest · Maintainer: Linus Torvalds

Possibly related (same subject, not in this thread)

Junio C Hamano [off-list ref] writes:
quoted
 	else if (argc < 3)
 		usage("test-tool regex --bug\n"
-		      "test-tool regex <pattern> <string> [<options>]");
+		      "test-tool regex [--silent] <pattern> <string> [<options>]");

+	if (!strcmp(argv[1], "--silent")) {
+		silent = 1;
+		argv++;
+	}
This looks fishy---if argc==3 and the first one is "--silent", only
the <pattern> is left in argv and before taking <string> out of the
argv, we need to ensure argc is still large enough, but I do not
think that is done below:
...
Not that it matters _too_ much as this is merely a test helper and
it would not hurt anybody as long as our callers are careful.
But it still bothers me.  Perhaps like this?  

If I were writing this from scratch, I would probably increment
argv++ once as early as possible and consistently access argv[0]
or *argv++, but that would make the patch even noisier.

 t/helper/test-regex.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/t/helper/test-regex.c b/t/helper/test-regex.c
index 7a8ddce45b..e68b4f6a73 100644
--- a/t/helper/test-regex.c
+++ b/t/helper/test-regex.c
@@ -46,16 +46,23 @@ int cmd__regex(int argc, const char **argv)
 	regmatch_t m[1];
 	char errbuf[64];
 
-	if (argc == 2 && !strcmp(argv[1], "--bug"))
-		return test_regex_bug();
-	else if (argc < 3)
-		usage("test-tool regex --bug\n"
-		      "test-tool regex [--silent] <pattern> <string> [<options>]");
+	if (argc < 2)
+		goto usage;
 
+	if (!strcmp(argv[1], "--bug")) {
+		if (argc == 2)
+			return test_regex_bug();
+		else
+			goto usage;
+	}
 	if (!strcmp(argv[1], "--silent")) {
-		silent = 1;
+		silent = 0;
 		argv++;
+		argc--;
 	}
+	if (argc < 3)
+		goto usage;
+
 	argv++;
 	pat = *argv++;
 	str = *argv++;
@@ -84,4 +91,8 @@ int cmd__regex(int argc, const char **argv)
 		return 1;
 
 	return 0;
+
+usage:
+	usage("test-tool regex --bug\n"
+	      "test-tool regex [--silent] <pattern> <string> [<options>]");
 }
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help