Skip to content

Commit f9b3c1f

Browse files
cloobTechgitster
authored andcommitted
environment: stop storing core.attributesFile globally
The `core.attributeFile` config value is parsed in git_default_core_config(), loaded eagerly and stored in the global variable `git_attributes_file`. Storing this value in a global variable can lead to it being overwritten by another repository when more than one Git repository run in the same Git process. Create a new struct `repo_config_values` to hold this value and other repository dependent values parsed by `git_default_config()`. This will ensure the current behaviour remains the same while also enabling the libification of Git. An accessor function 'repo_config_values()' s created to ensure that we do not access an uninitialized repository, or an instance of a different repository than the current one. Suggested-by: Phillip Wood <phillip.wood123@gmail.com> Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Usman Akinyemi <usmanakinyemi202@gmail.com> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Olamide Caleb Bello <belkid98@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 68cb7f9 commit f9b3c1f

6 files changed

Lines changed: 45 additions & 7 deletions

File tree

attr.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,10 +881,11 @@ const char *git_attr_system_file(void)
881881

882882
const char *git_attr_global_file(void)
883883
{
884-
if (!git_attributes_file)
885-
git_attributes_file = xdg_config_home("attributes");
884+
struct repo_config_values *cfg = repo_config_values(the_repository);
885+
if (!cfg->attributes_file)
886+
cfg->attributes_file = xdg_config_home("attributes");
886887

887-
return git_attributes_file;
888+
return cfg->attributes_file;
888889
}
889890

890891
int git_attr_system_is_enabled(void)

environment.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ char *git_commit_encoding;
5353
char *git_log_output_encoding;
5454
char *apply_default_whitespace;
5555
char *apply_default_ignorewhitespace;
56-
char *git_attributes_file;
5756
int zlib_compression_level = Z_BEST_SPEED;
5857
int pack_compression_level = Z_DEFAULT_COMPRESSION;
5958
int fsync_object_files = -1;
@@ -327,6 +326,8 @@ static enum fsync_component parse_fsync_components(const char *var, const char *
327326
static int git_default_core_config(const char *var, const char *value,
328327
const struct config_context *ctx, void *cb)
329328
{
329+
struct repo_config_values *cfg = repo_config_values(the_repository);
330+
330331
/* This needs a better name */
331332
if (!strcmp(var, "core.filemode")) {
332333
trust_executable_bit = git_config_bool(var, value);
@@ -364,8 +365,8 @@ static int git_default_core_config(const char *var, const char *value,
364365
}
365366

366367
if (!strcmp(var, "core.attributesfile")) {
367-
FREE_AND_NULL(git_attributes_file);
368-
return git_config_pathname(&git_attributes_file, var, value);
368+
FREE_AND_NULL(cfg->attributes_file);
369+
return git_config_pathname(&cfg->attributes_file, var, value);
369370
}
370371

371372
if (!strcmp(var, "core.bare")) {
@@ -756,3 +757,8 @@ int git_default_config(const char *var, const char *value,
756757
/* Add other config variables here and to Documentation/config.adoc. */
757758
return 0;
758759
}
760+
761+
void repo_config_values_init(struct repo_config_values *cfg)
762+
{
763+
cfg->attributes_file = NULL;
764+
}

environment.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ extern const char * const local_repo_env[];
8484

8585
struct strvec;
8686

87+
struct repository;
88+
struct repo_config_values {
89+
/* section "core" config values */
90+
char *attributes_file;
91+
};
92+
93+
struct repo_config_values *repo_config_values(struct repository *repo);
94+
8795
/*
8896
* Wrapper of getenv() that returns a strdup value. This value is kept
8997
* in argv to be freed later.
@@ -107,6 +115,8 @@ const char *strip_namespace(const char *namespaced_ref);
107115
int git_default_config(const char *, const char *,
108116
const struct config_context *, void *);
109117

118+
void repo_config_values_init(struct repo_config_values *cfg);
119+
110120
/*
111121
* TODO: All the below state either explicitly or implicitly relies on
112122
* `the_repository`. We should eventually get rid of these and make the
@@ -152,7 +162,6 @@ extern int assume_unchanged;
152162
extern int warn_on_object_refname_ambiguity;
153163
extern char *apply_default_whitespace;
154164
extern char *apply_default_ignorewhitespace;
155-
extern char *git_attributes_file;
156165
extern int zlib_compression_level;
157166
extern int pack_compression_level;
158167
extern unsigned long pack_size_limit_cfg;

oss-fuzz/fuzz-commit-graph.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
1010
{
1111
struct commit_graph *g;
1212

13+
memset(the_repository, 0, sizeof(*the_repository));
1314
initialize_repository(the_repository);
1415

1516
/*

repository.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,27 @@ static void set_default_hash_algo(struct repository *repo)
5050
repo_set_hash_algo(repo, algo);
5151
}
5252

53+
struct repo_config_values *repo_config_values(struct repository *repo)
54+
{
55+
if (repo != the_repository)
56+
BUG("trying to read config from wrong repository instance");
57+
if (!repo->initialized)
58+
BUG("config values from uninitialized repository");
59+
return &repo->config_values_private_;
60+
}
61+
5362
void initialize_repository(struct repository *repo)
5463
{
64+
if (repo->initialized)
65+
BUG("repository initialized already");
66+
repo->initialized = true;
67+
5568
repo->remote_state = remote_state_new();
5669
repo->parsed_objects = parsed_object_pool_new(repo);
5770
ALLOC_ARRAY(repo->index, 1);
5871
index_state_init(repo->index, repo);
5972
repo->check_deprecated_config = true;
73+
repo_config_values_init(&repo->config_values_private_);
6074

6175
/*
6276
* When a command runs inside a repository, it learns what

repository.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "strmap.h"
55
#include "repo-settings.h"
6+
#include "environment.h"
67

78
struct config_set;
89
struct git_hash_algo;
@@ -148,6 +149,9 @@ struct repository {
148149
/* Repository's compatibility hash algorithm. */
149150
const struct git_hash_algo *compat_hash_algo;
150151

152+
/* Repository's config values parsed by git_default_config() */
153+
struct repo_config_values config_values_private_;
154+
151155
/* Repository's reference storage format, as serialized on disk. */
152156
enum ref_storage_format ref_storage_format;
153157

@@ -171,6 +175,9 @@ struct repository {
171175

172176
/* Should repo_config() check for deprecated settings */
173177
bool check_deprecated_config;
178+
179+
/* Has this repository instance been initialized? */
180+
bool initialized;
174181
};
175182

176183
#ifdef USE_THE_REPOSITORY_VARIABLE

0 commit comments

Comments
 (0)