Skip to content

Commit ce992ce

Browse files
pks-tgitster
authored andcommitted
builtin/blame: don't access potentially unitialized the_hash_algo
We access `the_hash_algo` in git-blame(1) before we have executed `parse_options_start()`, which may not be properly set up in case we have no repository. This is fine for most of the part because all the call paths that lead to it (git-blame(1), git-annotate(1) as well as git-pick-axe(1)) specify `RUN_SETUP` and thus require a repository. There is one exception though, namely when passing `-h` to print the help. Here we will access `the_hash_algo` even if there is no repo. This works fine right now because `the_hash_algo` gets sets up to point to the SHA1 algorithm via `initialize_repository()`. But we're about to stop doing this, and thus the code would lead to a `NULL` pointer exception. Prepare the code for this and only access `the_hash_algo` after we are sure that there is a proper repository. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 07658e9 commit ce992ce

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

builtin/blame.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,6 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
915915
struct range_set ranges;
916916
unsigned int range_i;
917917
long anchor;
918-
const int hexsz = the_hash_algo->hexsz;
919918
long num_lines = 0;
920919
const char *str_usage = cmd_is_annotate ? annotate_usage : blame_usage;
921920
const char **opt_usage = cmd_is_annotate ? annotate_opt_usage : blame_opt_usage;
@@ -973,11 +972,11 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
973972
} else if (show_progress < 0)
974973
show_progress = isatty(2);
975974

976-
if (0 < abbrev && abbrev < hexsz)
975+
if (0 < abbrev && abbrev < (int)the_hash_algo->hexsz)
977976
/* one more abbrev length is needed for the boundary commit */
978977
abbrev++;
979978
else if (!abbrev)
980-
abbrev = hexsz;
979+
abbrev = the_hash_algo->hexsz;
981980

982981
if (revs_file && read_ancestry(revs_file))
983982
die_errno("reading graft file '%s' failed", revs_file);

0 commit comments

Comments
 (0)