Skip to content

Commit a60c21b

Browse files
committed
Merge branch 'ps/undecided-is-not-necessarily-sha1'
Before discovering the repository details, We used to assume SHA-1 as the "default" hash function, which has been corrected. Hopefully this will smoke out codepaths that rely on such an unwarranted assumptions. * ps/undecided-is-not-necessarily-sha1: repository: stop setting SHA1 as the default object hash oss-fuzz/commit-graph: set up hash algorithm builtin/shortlog: don't set up revisions without repo builtin/diff: explicitly set hash algo when there is no repo builtin/bundle: abort "verify" early when there is no repository builtin/blame: don't access potentially unitialized `the_hash_algo` builtin/rev-parse: allow shortening to more than 40 hex characters remote-curl: fix parsing of detached SHA256 heads attr: fix BUG() when parsing attrs outside of repo attr: don't recompute default attribute source parse-options-cb: only abbreviate hashes when hash algo is known path: move `validate_headref()` to its only user path: harden validation of HEAD with non-standard hashes
2 parents 3a57aa5 + c8aed5e commit a60c21b

17 files changed

+168
-92
lines changed

attr.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,15 +1205,16 @@ static void collect_some_attrs(struct index_state *istate,
12051205
}
12061206

12071207
static const char *default_attr_source_tree_object_name;
1208-
static int ignore_bad_attr_tree;
12091208

12101209
void set_git_attr_source(const char *tree_object_name)
12111210
{
12121211
default_attr_source_tree_object_name = xstrdup(tree_object_name);
12131212
}
12141213

1215-
static void compute_default_attr_source(struct object_id *attr_source)
1214+
static int compute_default_attr_source(struct object_id *attr_source)
12161215
{
1216+
int ignore_bad_attr_tree = 0;
1217+
12171218
if (!default_attr_source_tree_object_name)
12181219
default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT);
12191220

@@ -1222,22 +1223,34 @@ static void compute_default_attr_source(struct object_id *attr_source)
12221223
ignore_bad_attr_tree = 1;
12231224
}
12241225

1225-
if (!default_attr_source_tree_object_name || !is_null_oid(attr_source))
1226-
return;
1226+
if (!default_attr_source_tree_object_name)
1227+
return 0;
1228+
1229+
if (!startup_info->have_repository) {
1230+
if (!ignore_bad_attr_tree)
1231+
die(_("cannot use --attr-source or GIT_ATTR_SOURCE without repo"));
1232+
return 0;
1233+
}
12271234

12281235
if (repo_get_oid_treeish(the_repository,
12291236
default_attr_source_tree_object_name,
1230-
attr_source) && !ignore_bad_attr_tree)
1231-
die(_("bad --attr-source or GIT_ATTR_SOURCE"));
1237+
attr_source)) {
1238+
if (!ignore_bad_attr_tree)
1239+
die(_("bad --attr-source or GIT_ATTR_SOURCE"));
1240+
return 0;
1241+
}
1242+
1243+
return 1;
12321244
}
12331245

12341246
static struct object_id *default_attr_source(void)
12351247
{
12361248
static struct object_id attr_source;
1249+
static int has_attr_source = -1;
12371250

1238-
if (is_null_oid(&attr_source))
1239-
compute_default_attr_source(&attr_source);
1240-
if (is_null_oid(&attr_source))
1251+
if (has_attr_source < 0)
1252+
has_attr_source = compute_default_attr_source(&attr_source);
1253+
if (!has_attr_source)
12411254
return NULL;
12421255
return &attr_source;
12431256
}

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);

builtin/bundle.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
140140
builtin_bundle_verify_usage, options, &bundle_file);
141141
/* bundle internals use argv[1] as further parameters */
142142

143+
if (!startup_info->have_repository) {
144+
ret = error(_("need a repository to verify a bundle"));
145+
goto cleanup;
146+
}
147+
143148
if ((bundle_fd = open_bundle(bundle_file, &header, &name)) < 0) {
144149
ret = 1;
145150
goto cleanup;

builtin/diff.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,15 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
465465
no_index = DIFF_NO_INDEX_IMPLICIT;
466466
}
467467

468+
/*
469+
* When operating outside of a Git repository we need to have a hash
470+
* algorithm at hand so that we can generate the blob hashes. We
471+
* default to SHA1 here, but may eventually want to change this to be
472+
* configurable via a command line option.
473+
*/
474+
if (nongit)
475+
repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
476+
468477
init_diff_ui_defaults();
469478
git_config(git_diff_ui_config, NULL);
470479
prefix = precompose_argv_prefix(argc, argv, prefix);

builtin/rev-parse.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,6 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
691691
const char *name = NULL;
692692
struct object_context unused;
693693
struct strbuf buf = STRBUF_INIT;
694-
const int hexsz = the_hash_algo->hexsz;
695694
int seen_end_of_options = 0;
696695
enum format_type format = FORMAT_DEFAULT;
697696

@@ -867,8 +866,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
867866
abbrev = strtoul(arg, NULL, 10);
868867
if (abbrev < MINIMUM_ABBREV)
869868
abbrev = MINIMUM_ABBREV;
870-
else if (hexsz <= abbrev)
871-
abbrev = hexsz;
869+
else if ((int)the_hash_algo->hexsz <= abbrev)
870+
abbrev = the_hash_algo->hexsz;
872871
continue;
873872
}
874873
if (!strcmp(arg, "--sq")) {

builtin/shortlog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
435435
usage_with_options(shortlog_usage, options);
436436
}
437437

438-
if (setup_revisions(argc, argv, &rev, NULL) != 1) {
438+
if (!nongit && setup_revisions(argc, argv, &rev, NULL) != 1) {
439439
error(_("unrecognized argument: %s"), argv[1]);
440440
usage_with_options(shortlog_usage, options);
441441
}

oss-fuzz/fuzz-commit-graph.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
1919
* touching the disk to keep the individual fuzz-test cases as fast as
2020
* possible.
2121
*/
22+
repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
2223
the_repository->settings.commit_graph_generation_version = 2;
2324
the_repository->settings.commit_graph_read_changed_paths = 1;
2425
g = parse_commit_graph(&the_repository->settings, (void *)data, size);

parse-options-cb.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "environment.h"
88
#include "gettext.h"
99
#include "object-name.h"
10+
#include "setup.h"
1011
#include "string-list.h"
1112
#include "strvec.h"
1213
#include "oid-array.h"
@@ -29,7 +30,7 @@ int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
2930
opt->long_name);
3031
if (v && v < MINIMUM_ABBREV)
3132
v = MINIMUM_ABBREV;
32-
else if (v > the_hash_algo->hexsz)
33+
else if (startup_info->have_repository && v > the_hash_algo->hexsz)
3334
v = the_hash_algo->hexsz;
3435
}
3536
*(int *)(opt->value) = v;

path.c

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "abspath.h"
66
#include "environment.h"
77
#include "gettext.h"
8-
#include "hex.h"
98
#include "repository.h"
109
#include "strbuf.h"
1110
#include "string-list.h"
@@ -647,58 +646,6 @@ void strbuf_git_common_path(struct strbuf *sb,
647646
va_end(args);
648647
}
649648

650-
int validate_headref(const char *path)
651-
{
652-
struct stat st;
653-
char buffer[256];
654-
const char *refname;
655-
struct object_id oid;
656-
int fd;
657-
ssize_t len;
658-
659-
if (lstat(path, &st) < 0)
660-
return -1;
661-
662-
/* Make sure it is a "refs/.." symlink */
663-
if (S_ISLNK(st.st_mode)) {
664-
len = readlink(path, buffer, sizeof(buffer)-1);
665-
if (len >= 5 && !memcmp("refs/", buffer, 5))
666-
return 0;
667-
return -1;
668-
}
669-
670-
/*
671-
* Anything else, just open it and try to see if it is a symbolic ref.
672-
*/
673-
fd = open(path, O_RDONLY);
674-
if (fd < 0)
675-
return -1;
676-
len = read_in_full(fd, buffer, sizeof(buffer)-1);
677-
close(fd);
678-
679-
if (len < 0)
680-
return -1;
681-
buffer[len] = '\0';
682-
683-
/*
684-
* Is it a symbolic ref?
685-
*/
686-
if (skip_prefix(buffer, "ref:", &refname)) {
687-
while (isspace(*refname))
688-
refname++;
689-
if (starts_with(refname, "refs/"))
690-
return 0;
691-
}
692-
693-
/*
694-
* Is this a detached HEAD?
695-
*/
696-
if (!get_oid_hex(buffer, &oid))
697-
return 0;
698-
699-
return -1;
700-
}
701-
702649
static struct passwd *getpw_str(const char *username, size_t len)
703650
{
704651
struct passwd *pw;

path.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ const char *git_path_fetch_head(struct repository *r);
173173
const char *git_path_shallow(struct repository *r);
174174

175175
int ends_with_path_components(const char *path, const char *components);
176-
int validate_headref(const char *ref);
177176

178177
int calc_shared_perm(int mode);
179178
int adjust_shared_perm(const char *path);

0 commit comments

Comments
 (0)