Skip to content

Commit 1b4c57f

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Reviewed-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 24b7d1e commit 1b4c57f

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

t/helper/test-bloom.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,21 @@ static void get_bloom_filter_for_commit(const struct object_id *commit_oid)
4343
print_bloom_filter(filter);
4444
}
4545

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+
4651
int cmd__bloom(int argc, const char **argv)
4752
{
53+
if (argc < 2)
54+
usage(bloom_usage);
55+
4856
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]));
5061
printf("Murmur3 Hash with seed=0:0x%08x\n", hashed);
5162
}
5263

@@ -56,9 +67,8 @@ int cmd__bloom(int argc, const char **argv)
5667
filter.len = (settings.bits_per_entry + BITS_PER_WORD - 1) / BITS_PER_WORD;
5768
filter.data = xcalloc(filter.len, sizeof(unsigned char));
5869

59-
if (!argv[2]) {
60-
die("at least one input string expected");
61-
}
70+
if (argc - 1 < i)
71+
usage(bloom_usage);
6272

6373
while (argv[i]) {
6474
add_string_to_filter(argv[i], &filter);
@@ -71,6 +81,8 @@ int cmd__bloom(int argc, const char **argv)
7181
if (!strcmp(argv[1], "get_filter_for_commit")) {
7282
struct object_id oid;
7383
const char *end;
84+
if (argc < 3)
85+
usage(bloom_usage);
7486
if (parse_oid_hex(argv[2], &oid, &end))
7587
die("cannot parse oid '%s'", argv[2]);
7688
init_bloom_filters();

0 commit comments

Comments
 (0)