Derrick Stolee <sto...@gmail.com> writes:

> +     devnull = open("/dev/null", O_WRONLY);
> +     f = hashfd(devnull, NULL);
> +     hashwrite(f, g->data, g->data_len - g->hash_len);

I wondered if we can hide the knowledge of "/dev/null" by reusing
hashfd_check() from csum-file.c (which is used by the verification
of pack idx file), which also gives us the hash comparison of an
existing file on disk for free.  But unfortunately this codepath
only verifies the checksum and not contents, so we cannot avoid
setting up the /dev/null sink manually like this patch does, I
guess.

> +     finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
> +     if (hashcmp(checksum.hash, g->data + g->data_len - g->hash_len)) {
> +             graph_report(_("the commit-graph file has incorrect checksum 
> and is likely corrupt"));
> +             verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
> +     }
> +
>       for (i = 0; i < g->num_commits; i++) {
>               struct commit *graph_commit;
>  
> @@ -909,7 +921,7 @@ int verify_commit_graph(struct repository *r, struct 
> commit_graph *g)
>               cur_fanout_pos++;
>       }
>  
> -     if (verify_commit_graph_error)
> +     if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
>               return verify_commit_graph_error;
>  
>       for (i = 0; i < g->num_commits; i++) {
> diff --git a/t/t5318-commit-graph.sh b/t/t5318-commit-graph.sh
> index a0cf1f66de..fed05e2f12 100755
> --- a/t/t5318-commit-graph.sh
> +++ b/t/t5318-commit-graph.sh
> @@ -279,6 +279,7 @@ GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
>  GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
>                            $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
>  GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
> +GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
>  
>  # usage: corrupt_graph_and_verify <position> <data> <string>
>  # Manipulates the commit-graph file at the position
> @@ -393,4 +394,9 @@ test_expect_success 'detect incorrect parent for octopus 
> merge' '
>               "invalid parent"
>  '
>  
> +test_expect_success 'detect invalid checksum hash' '
> +     corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
> +             "incorrect checksum"
> +'
> +
>  test_done

Reply via email to