pack-objects: clamp negative depth to 0

A negative delta depth makes no sense, and the code is not prepared to handle it. If passed "--depth=-1" on the command line, then this line from break_delta_chains(): cur->depth = (total_depth--) % (depth + 1); triggers a divide-by-zero. This is undefined behavior according to the C standard, but on POSIX systems results in SIGFPE killing the process. This is certainly one way to inform the use that the command was invalid, but it's a bit friendlier to just treat it as "don't allow any deltas", which we already do for --depth=0. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 1, 2021 at 10:04 UTC 6d52b6a5dfe0bee20dbfb2a3a7291bcf9a152672
2 files changed +9
builtin/pack-objects.c
+2
@@ -3861,6 +3861,8 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
3861 if (pack_to_stdout != !base_name || argc)
3862 usage_with_options(pack_usage, pack_objects_options);
3863
3864 + if (depth < 0)
3865 + depth = 0;
3866 if (depth >= (1 << OE_DEPTH_BITS)) {
3867 warning(_("delta chain depth %d is too deep, forcing %d"),
3868 depth, (1 << OE_DEPTH_BITS) - 1);
t/t5316-pack-delta-depth.sh
+7
@@ -102,4 +102,11 @@ test_expect_success '--depth=0 disables deltas' '
102 test_cmp expect actual
103 '
104
105 +test_expect_success 'negative depth disables deltas' '
106 + pack=$(git pack-objects --all --depth=-1 </dev/null pack) &&
107 + echo 0 >expect &&
108 + max_chain pack-$pack.pack >actual &&
109 + test_cmp expect actual
110 +'
111 +
112 test_done