Skip to content

Commit 49d6cfa

Browse files
peffgitster
authored andcommitted
config: do not use C function names as struct members
According to C99, section 7.1.4: Any function declared in a header may be additionally implemented as a function-like macro defined in the header. Therefore calling our struct member function pointer "fgetc" may run afoul of unwanted macro expansion when we call: char c = cf->fgetc(cf); This turned out to be a problem on uclibc, which defines fgetc as a macro and causes compilation failure. The standard suggests fixing this in a few ways: 1. Using extra parentheses to inhibit the function-like macro expansion. E.g., "(cf->fgetc)(cf)". This is undesirable as it's ugly, and each call site needs to remember to use it (and on systems without the macro, forgetting will compile just fine). 2. Using #undef (because a conforming implementation must also be providing fgetc as a function). This is undesirable because presumably the implementation was using the macro for a performance benefit, and we are dropping that optimization. Instead, we can simply use non-colliding names. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b2dc094 commit 49d6cfa

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

config.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ struct config_source {
2727
struct strbuf value;
2828
struct strbuf var;
2929

30-
int (*fgetc)(struct config_source *c);
31-
int (*ungetc)(int c, struct config_source *conf);
32-
long (*ftell)(struct config_source *c);
30+
int (*do_fgetc)(struct config_source *c);
31+
int (*do_ungetc)(int c, struct config_source *conf);
32+
long (*do_ftell)(struct config_source *c);
3333
};
3434

3535
static struct config_source *cf;
@@ -217,13 +217,13 @@ int git_config_from_parameters(config_fn_t fn, void *data)
217217

218218
static int get_next_char(void)
219219
{
220-
int c = cf->fgetc(cf);
220+
int c = cf->do_fgetc(cf);
221221

222222
if (c == '\r') {
223223
/* DOS like systems */
224-
c = cf->fgetc(cf);
224+
c = cf->do_fgetc(cf);
225225
if (c != '\n') {
226-
cf->ungetc(c, cf);
226+
cf->do_ungetc(c, cf);
227227
c = '\r';
228228
}
229229
}
@@ -982,9 +982,9 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
982982
top.u.file = f;
983983
top.name = filename;
984984
top.die_on_error = 1;
985-
top.fgetc = config_file_fgetc;
986-
top.ungetc = config_file_ungetc;
987-
top.ftell = config_file_ftell;
985+
top.do_fgetc = config_file_fgetc;
986+
top.do_ungetc = config_file_ungetc;
987+
top.do_ftell = config_file_ftell;
988988

989989
ret = do_config_from(&top, fn, data);
990990

@@ -1003,9 +1003,9 @@ int git_config_from_buf(config_fn_t fn, const char *name, const char *buf,
10031003
top.u.buf.pos = 0;
10041004
top.name = name;
10051005
top.die_on_error = 0;
1006-
top.fgetc = config_buf_fgetc;
1007-
top.ungetc = config_buf_ungetc;
1008-
top.ftell = config_buf_ftell;
1006+
top.do_fgetc = config_buf_fgetc;
1007+
top.do_ungetc = config_buf_ungetc;
1008+
top.do_ftell = config_buf_ftell;
10091009

10101010
return do_config_from(&top, fn, data);
10111011
}
@@ -1186,7 +1186,7 @@ static int store_aux(const char *key, const char *value, void *cb)
11861186
return 1;
11871187
}
11881188

1189-
store.offset[store.seen] = cf->ftell(cf);
1189+
store.offset[store.seen] = cf->do_ftell(cf);
11901190
store.seen++;
11911191
}
11921192
break;
@@ -1213,19 +1213,19 @@ static int store_aux(const char *key, const char *value, void *cb)
12131213
* Do not increment matches: this is no match, but we
12141214
* just made sure we are in the desired section.
12151215
*/
1216-
store.offset[store.seen] = cf->ftell(cf);
1216+
store.offset[store.seen] = cf->do_ftell(cf);
12171217
/* fallthru */
12181218
case SECTION_END_SEEN:
12191219
case START:
12201220
if (matches(key, value)) {
1221-
store.offset[store.seen] = cf->ftell(cf);
1221+
store.offset[store.seen] = cf->do_ftell(cf);
12221222
store.state = KEY_SEEN;
12231223
store.seen++;
12241224
} else {
12251225
if (strrchr(key, '.') - key == store.baselen &&
12261226
!strncmp(key, store.key, store.baselen)) {
12271227
store.state = SECTION_SEEN;
1228-
store.offset[store.seen] = cf->ftell(cf);
1228+
store.offset[store.seen] = cf->do_ftell(cf);
12291229
}
12301230
}
12311231
}

0 commit comments

Comments
 (0)