Skip to content

Commit 924aaf3

Browse files
Ramsay Jonesgitster
authored andcommitted
config.c: Make git_config() work correctly when called recursively
On Cygwin, this fixes a test failure in t3301-notes.sh (test 98, "git notes copy --for-rewrite (disabled)"). The test failure is caused by a recursive call to git_config() which has the effect of skipping to the end-of-file while processing the "notes.rewriteref" config variable. Thus, any config variables that appear after "notes.rewriteref" are simply ignored by git_config(). Also, we note that the original FILE handle is leaked as a result of the recursive call. The recursive call to git_config() is due to the "schizophrenic stat" functions on cygwin, where one of two different implementations of the l/stat functions is selected lazily, depending on some config variables. In this case, the init_copy_notes_for_rewrite() function calls git_config() with the notes_rewrite_config() callback function. This callback, while processing the "notes.rewriteref" variable, in turn calls string_list_add_refs_by_glob() to process the associated ref value. This eventually leads to a call to the get_ref_dir() function, which in turn calls stat(). On cygwin, the stat() macro leads to an indirect call to cygwin_stat_stub() which, via init_stat(), then calls git_config() in order to determine which l/stat implementation to bind to. In order to solve this problem, we modify git_config() so that the global state variables used by the config reading code is packaged up and managed on a local state stack. Signed-off-by: Ramsay Jones <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 452993c commit 924aaf3

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

config.c

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212

1313
#define MAXNAME (256)
1414

15-
static FILE *config_file;
16-
static const char *config_file_name;
17-
static int config_linenr;
18-
static int config_file_eof;
15+
typedef struct config_file {
16+
struct config_file *prev;
17+
FILE *f;
18+
const char *name;
19+
int linenr;
20+
int eof;
21+
struct strbuf value;
22+
char var[MAXNAME];
23+
} config_file;
24+
25+
static config_file *cf;
26+
1927
static int zlib_compression_seen;
2028

2129
const char *config_exclusive_filename = NULL;
@@ -99,7 +107,7 @@ static int get_next_char(void)
99107
FILE *f;
100108

101109
c = '\n';
102-
if ((f = config_file) != NULL) {
110+
if (cf && ((f = cf->f) != NULL)) {
103111
c = fgetc(f);
104112
if (c == '\r') {
105113
/* DOS like systems */
@@ -110,9 +118,9 @@ static int get_next_char(void)
110118
}
111119
}
112120
if (c == '\n')
113-
config_linenr++;
121+
cf->linenr++;
114122
if (c == EOF) {
115-
config_file_eof = 1;
123+
cf->eof = 1;
116124
c = '\n';
117125
}
118126
}
@@ -121,21 +129,20 @@ static int get_next_char(void)
121129

122130
static char *parse_value(void)
123131
{
124-
static struct strbuf value = STRBUF_INIT;
125132
int quote = 0, comment = 0, space = 0;
126133

127-
strbuf_reset(&value);
134+
strbuf_reset(&cf->value);
128135
for (;;) {
129136
int c = get_next_char();
130137
if (c == '\n') {
131138
if (quote)
132139
return NULL;
133-
return value.buf;
140+
return cf->value.buf;
134141
}
135142
if (comment)
136143
continue;
137144
if (isspace(c) && !quote) {
138-
if (value.len)
145+
if (cf->value.len)
139146
space++;
140147
continue;
141148
}
@@ -146,7 +153,7 @@ static char *parse_value(void)
146153
}
147154
}
148155
for (; space; space--)
149-
strbuf_addch(&value, ' ');
156+
strbuf_addch(&cf->value, ' ');
150157
if (c == '\\') {
151158
c = get_next_char();
152159
switch (c) {
@@ -168,14 +175,14 @@ static char *parse_value(void)
168175
default:
169176
return NULL;
170177
}
171-
strbuf_addch(&value, c);
178+
strbuf_addch(&cf->value, c);
172179
continue;
173180
}
174181
if (c == '"') {
175182
quote = 1-quote;
176183
continue;
177184
}
178-
strbuf_addch(&value, c);
185+
strbuf_addch(&cf->value, c);
179186
}
180187
}
181188

@@ -192,7 +199,7 @@ static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
192199
/* Get the full name */
193200
for (;;) {
194201
c = get_next_char();
195-
if (config_file_eof)
202+
if (cf->eof)
196203
break;
197204
if (!iskeychar(c))
198205
break;
@@ -256,7 +263,7 @@ static int get_base_var(char *name)
256263

257264
for (;;) {
258265
int c = get_next_char();
259-
if (config_file_eof)
266+
if (cf->eof)
260267
return -1;
261268
if (c == ']')
262269
return baselen;
@@ -274,7 +281,7 @@ static int git_parse_file(config_fn_t fn, void *data)
274281
{
275282
int comment = 0;
276283
int baselen = 0;
277-
static char var[MAXNAME];
284+
char *var = cf->var;
278285

279286
/* U+FEFF Byte Order Mark in UTF8 */
280287
static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
@@ -298,7 +305,7 @@ static int git_parse_file(config_fn_t fn, void *data)
298305
}
299306
}
300307
if (c == '\n') {
301-
if (config_file_eof)
308+
if (cf->eof)
302309
return 0;
303310
comment = 0;
304311
continue;
@@ -323,7 +330,7 @@ static int git_parse_file(config_fn_t fn, void *data)
323330
if (get_value(fn, data, var, baselen+1) < 0)
324331
break;
325332
}
326-
die("bad config file line %d in %s", config_linenr, config_file_name);
333+
die("bad config file line %d in %s", cf->linenr, cf->name);
327334
}
328335

329336
static int parse_unit_factor(const char *end, unsigned long *val)
@@ -374,8 +381,8 @@ int git_parse_ulong(const char *value, unsigned long *ret)
374381

375382
static void die_bad_config(const char *name)
376383
{
377-
if (config_file_name)
378-
die("bad config value for '%s' in %s", name, config_file_name);
384+
if (cf && cf->name)
385+
die("bad config value for '%s' in %s", name, cf->name);
379386
die("bad config value for '%s'", name);
380387
}
381388

@@ -795,13 +802,24 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
795802

796803
ret = -1;
797804
if (f) {
798-
config_file = f;
799-
config_file_name = filename;
800-
config_linenr = 1;
801-
config_file_eof = 0;
805+
config_file top;
806+
807+
/* push config-file parsing state stack */
808+
top.prev = cf;
809+
top.f = f;
810+
top.name = filename;
811+
top.linenr = 1;
812+
top.eof = 0;
813+
strbuf_init(&top.value, 1024);
814+
cf = &top;
815+
802816
ret = git_parse_file(fn, data);
817+
818+
/* pop config-file parsing state stack */
819+
strbuf_release(&top.value);
820+
cf = top.prev;
821+
803822
fclose(f);
804-
config_file_name = NULL;
805823
}
806824
return ret;
807825
}
@@ -909,6 +927,7 @@ static int store_aux(const char *key, const char *value, void *cb)
909927
{
910928
const char *ep;
911929
size_t section_len;
930+
FILE *f = cf->f;
912931

913932
switch (store.state) {
914933
case KEY_SEEN:
@@ -920,7 +939,7 @@ static int store_aux(const char *key, const char *value, void *cb)
920939
return 1;
921940
}
922941

923-
store.offset[store.seen] = ftell(config_file);
942+
store.offset[store.seen] = ftell(f);
924943
store.seen++;
925944
}
926945
break;
@@ -947,19 +966,19 @@ static int store_aux(const char *key, const char *value, void *cb)
947966
* Do not increment matches: this is no match, but we
948967
* just made sure we are in the desired section.
949968
*/
950-
store.offset[store.seen] = ftell(config_file);
969+
store.offset[store.seen] = ftell(f);
951970
/* fallthru */
952971
case SECTION_END_SEEN:
953972
case START:
954973
if (matches(key, value)) {
955-
store.offset[store.seen] = ftell(config_file);
974+
store.offset[store.seen] = ftell(f);
956975
store.state = KEY_SEEN;
957976
store.seen++;
958977
} else {
959978
if (strrchr(key, '.') - key == store.baselen &&
960979
!strncmp(key, store.key, store.baselen)) {
961980
store.state = SECTION_SEEN;
962-
store.offset[store.seen] = ftell(config_file);
981+
store.offset[store.seen] = ftell(f);
963982
}
964983
}
965984
}
@@ -1415,6 +1434,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
14151434
struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
14161435
int out_fd;
14171436
char buf[1024];
1437+
FILE *config_file;
14181438

14191439
if (config_exclusive_filename)
14201440
config_filename = xstrdup(config_exclusive_filename);

0 commit comments

Comments
 (0)