Skip to contentSkip to conversation

bundle-uri.c: Fix double increment in depth

PATCH In review Toon Claes 4 participants 4 messages Jun 21, 2024

lore
Share & tools
lore

Toon Claes

author
#0 lore raw

PATCH 1/1 bundle-uri.c: Fix double increment in depth

A bundle URI can serve a gitformat-bundle(5) or a bundle list. This plain text file is in the Git config format containing other bundle URIs. To avoid these bundle lists to nest too deep, we've set a limit with max_bundle_uri_depth. Although, when walk through the tree of bundles, the current depth is incremented in download_bundle_list() and then calls download_bundle_to_file(), which also increments the depth. Remove the increment in download_bundle_to_file().

---
 bundle-uri.c                |  2 +-
 t/t5558-clone-bundle-uri.sh | 62 +++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 1 deletion(-)
bundle-uri.c +1 -1
index 91b3319a5c..7b1a711919 100644
@@ -436,7 +436,7 @@ static int download_bundle_to_file(struct remote_bundle_info *bundle, void *data
 	if (ctx->mode == BUNDLE_MODE_ANY && ctx->count)
 		return 0;
 
-	res = fetch_bundle_uri_internal(ctx->r, bundle, ctx->depth + 1, ctx->list);
+	res = fetch_bundle_uri_internal(ctx->r, bundle, ctx->depth, ctx->list);
 
 	/*
 	 * Only increment count if the download succeeded. If our mode is
t/t5558-clone-bundle-uri.sh +62 -0

68 lines not on this page · raw message

--
2.45.0

#1 lore raw
Quoted 5 lines · Toon Claes

A bundle URI can serve a gitformat-bundle(5) or a bundle list. This plain text file is in the Git config format containing other bundle URIs. To avoid these bundle lists to nest too deep, we've set a limit with max_bundle_uri_depth.

Yeah, max_bundle_uri_depth seems to be hardcoded to 4.

Quoted 1 line

Although, when walk through the tree of

s/walk/walking/

Quoted 2 lines

bundles, the current depth is incremented in download_bundle_list() and then calls download_bundle_to_file(), which also increments the depth.

s/and then calls/which then calls/

Quoted 1 line

Remove the increment in download_bundle_to_file().

The increment is removed by replacing:

    fetch_bundle_uri_internal( ..., ctx->depth + 1, ...)

with:

    fetch_bundle_uri_internal( ..., ctx->depth, ...)

in download_bundle_to_file(). Ok.

It looks like there is another similar call to that function like this:

fetch_bundle_uri_internal( ... , ctx.depth + 1, ... )

in fetch_bundles_by_token() though.

There ctx.depth is initialized to 0 before the call, so it looks like it could work, but fetch_bundle_uri_internal() can call fetch_bundle_list_in_config_format() which can call download_bundle_list() which, as we saw above, still increases the depth by 1.

So even if download_bundle_list() then calls download_bundle_to_file() without increasing the depth, I am not sure it works well in all cases. At least I think a bit more explanations might be needed.

Quoted 25 lines
t/t5558-clone-bundle-uri.sh +25 -0
+test_expect_success 'clone bundle list (file, above max depth)' '
+       cat >bundle-list-1 <<-EOF &&
+       [bundle]
+               version = 1
+               mode = any
+
+       [bundle "bundle-list-2"]
+               uri = file://$(pwd)/bundle-list-2
+       EOF
+
+       cat >bundle-list-2 <<-EOF &&
+       [bundle]
+               version = 1
+               mode = any
+
+       [bundle "bundle-list-3"]
+               uri = file://$(pwd)/bundle-list-3
+       EOF
+
+       cat >bundle-list-3 <<-EOF &&
+       [bundle]
+               version = 1
+               mode = any
+
+       [bundle "bundle-list-4"]

t/t5558-clone-bundle-uri.sh test_expect_success 'clone bundle list (file, any mode, all failures)' ' · line 296

Quoted 10 lines
t/t5558-clone-bundle-uri.sh +10 -0
+               uri = file://$(pwd)/bundle-list-4
+       EOF
+
+       cat >bundle-list-4 <<-EOF &&
+       [bundle]
+               version = 1
+               mode = any
+
+       [bundle "bundle-0"]
+               uri = file://$(pwd)/clone-from/bundle-0.bundle

Is there a reason why it's not more like:

       [bundle "bundle-list-5"]
               uri = file://$(pwd)/bundle-list-5

?

Quoted 1 line

+ EOF

It looks like the above is the setup part of the following tests, so it could perhaps be moved into a separate test_expect_success 'setup deep clone bundle list' test.

Quoted 14 lines
t/t5558-clone-bundle-uri.sh +14 -0
+       git clone --bundle-uri="file://$(pwd)/bundle-list-1" \
+               clone-from clone-too-deep 2>err &&
+       ! grep "fatal" err &&
+       grep "warning: exceeded bundle URI recursion limit" err &&
+
+       git -C clone-from for-each-ref --format="%(objectname)" >oids &&
+       git -C clone-too-deep cat-file --batch-check <oids &&
+
+       git -C clone-too-deep for-each-ref --format="%(refname)" >refs &&
+       ! grep "refs/bundles/" refs
+'
+
+test_expect_success 'clone bundle list (file, below max depth)' '
+       git clone --bundle-uri="file://$(pwd)/bundle-list-2" \

t/t5558-clone-bundle-uri.sh test_expect_success 'clone bundle list (file, any mode, all failures)' ' · line 322

Quoted 10 lines
t/t5558-clone-bundle-uri.sh +10 -0
+               clone-from clone-max-depth 2>err &&
+       ! grep "fatal" err &&
+       ! grep "warning: exceeded bundle URI recursion limit" err &&
+
+       git -C clone-from for-each-ref --format="%(objectname)" >oids &&
+       git -C clone-max-depth cat-file --batch-check <oids &&
+
+       git -C clone-max-depth for-each-ref --format="%(refname)" >refs &&
+       ! grep "refs/bundles/" refs
+'

Thanks!

#2 lore raw
Quoted 7 lines · Christian Couder
Quoted 5 lines · Toon Claes

A bundle URI can serve a gitformat-bundle(5) or a bundle list. This plain text file is in the Git config format containing other bundle URIs. To avoid these bundle lists to nest too deep, we've set a limit with max_bundle_uri_depth.

Quoted 5 lines

Yeah, max_bundle_uri_depth seems to be hardcoded to 4.

Quoted 1 line

Although, when walk through the tree of

s/walk/walking/

While you are typofixing ...

Quoted 1 line

Subject: Re: [PATCH] bundle-uri.c: Fix double increment in depth

... also "Fix" -> "fix".

Quoted 15 lines
Quoted 2 lines

bundles, the current depth is incremented in download_bundle_list() and then calls download_bundle_to_file(), which also increments the depth.

s/and then calls/which then calls/

Quoted 1 line

Remove the increment in download_bundle_to_file().

The increment is removed by replacing:

    fetch_bundle_uri_internal( ..., ctx->depth + 1, ...)

with:

    fetch_bundle_uri_internal( ..., ctx->depth, ...)
Quoted 7 lines

in download_bundle_to_file(). Ok.

It looks like there is another similar call to that function like this:

fetch_bundle_uri_internal( ... , ctx.depth + 1, ... )

in fetch_bundles_by_token() though.

I have to wonder if the code should pass the whole ctx around, instead of passing depth separately, and increment it at the single place that matters, in order to reduce the chance of similar problem happening. The place that matters the recursion depth can be the download_bundle_list() function---that is the one that controls the recursion, and it is incrementing the depth for the calls it makes (via the for_all_* callback mechanism). Alternatively, it can be the fetch_bundle_uri_internal() function where actual copying, for which we do want to enforce the depth limit, happens. The function even has the code for depth limit, so having an increment next to it may make it more readable and understandable.

So instead of taking ctx->r, ctx->depth+1, and ctx->list separately, shouldn't fetch_bundle_uri_internal() take the whole ctx and use ctx->depth (not +1---incrementing it is not its business) and the whole (current and future) problem like this goes away, no?

#3 lore raw
In reply to Toon Claes
Quoted 21 lines · Toon Claes

A bundle URI can serve a gitformat-bundle(5) or a bundle list. This plain text file is in the Git config format containing other bundle URIs. To avoid these bundle lists to nest too deep, we've set a limit with max_bundle_uri_depth. Although, when walk through the tree of bundles, the current depth is incremented in download_bundle_list() and then calls download_bundle_to_file(), which also increments the depth. Remove the increment in download_bundle_to_file().

---

t/t5558-clone-bundle-uri.sh +9 -0
@@ -259,6 +259,68 @@ test_expect_success 'clone bundle list (file, any mode, all failures)' '
+test_expect_success 'clone bundle list (file, above max depth)' '
+       cat >bundle-list-1 <<-EOF &&
+       [bundle]
+               version = 1
+               mode = any
+
+       [bundle "bundle-list-2"]
+               uri = file://$(pwd)/bundle-list-2
+       EOF

t/t5558-clone-bundle-uri.sh test_expect_success 'clone bundle list (file, any mode, all failures)' ' · line 277

Quoted 10 lines
t/t5558-clone-bundle-uri.sh +10 -0
+
+       cat >bundle-list-2 <<-EOF &&
+       [bundle]
+               version = 1
+               mode = any
+
+       [bundle "bundle-list-3"]
+               uri = file://$(pwd)/bundle-list-3
+       EOF
+ [...]

A very minor (and subjective) comment beyond those offered by other reviewers...

The above "setup" functionality can be collapsed to:

    for i in $(test_seq 4)
    do
        cat >bundle-list-$i <<-EOF || return 1
        [bundle]
            version = 1
            mode = any
        [bundle "bundle-list-$(($i + 1))"]
            uri = file://$(pwd)/bundle-list-$(($i + 1))
        EOF
    done