@matheusbsbs / matheus-bagliano / commits / 1b4c57fa87
Fork 0

test-bloom: check that we have expected arguments

If "test-tool bloom" is not fed a command, or if arguments are missing for some commands, it will just segfault. Let's check argc and write a friendlier usage message. Signed-off-by: Jeff King <peff@peff.net> Reviewed-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 23, 2020 at 16:59 UTC 1b4c57fa87e121f155863f898dc39d06cf4a1d99
1 file changed +16 -4
t/helper/test-bloom.c
+16 -4
@@ -43,10 +43,21 @@ static void get_bloom_filter_for_commit(const struct object_id *commit_oid)
43 print_bloom_filter(filter);
44 }
45
46 +static const char *bloom_usage = "\n"
47 +" test-tool bloom get_murmer3 <string>\n"
48 +" test-tool bloom generate_filter <string> [<string>...]\n"
49 +" test-tool get_filter_for_commit <commit-hex>\n";
50 +
51 int cmd__bloom(int argc, const char **argv)
52 {
53 + if (argc < 2)
54 + usage(bloom_usage);
55 +
56 if (!strcmp(argv[1], "get_murmur3")) {
49 - uint32_t hashed = murmur3_seeded(0, argv[2], strlen(argv[2]));
57 + uint32_t hashed;
58 + if (argc < 3)
59 + usage(bloom_usage);
60 + hashed = murmur3_seeded(0, argv[2], strlen(argv[2]));
61 printf("Murmur3 Hash with seed=0:0x%08x\n", hashed);
62 }
63
@@ -56,9 +67,8 @@ int cmd__bloom(int argc, const char **argv)
67 filter.len = (settings.bits_per_entry + BITS_PER_WORD - 1) / BITS_PER_WORD;
68 filter.data = xcalloc(filter.len, sizeof(unsigned char));
69
59 - if (!argv[2]) {
60 - die("at least one input string expected");
61 - }
70 + if (argc - 1 < i)
71 + usage(bloom_usage);
72
73 while (argv[i]) {
74 add_string_to_filter(argv[i], &filter);
@@ -71,6 +81,8 @@ int cmd__bloom(int argc, const char **argv)
81 if (!strcmp(argv[1], "get_filter_for_commit")) {
82 struct object_id oid;
83 const char *end;
84 + if (argc < 3)
85 + usage(bloom_usage);
86 if (parse_oid_hex(argv[2], &oid, &end))
87 die("cannot parse oid '%s'", argv[2]);
88 init_bloom_filters();