Skip to content

Commit e91cfe6

Browse files
ttaylorrpeff
authored andcommitted
config.c: avoid integer truncation in copy_or_rename_section_in_file()
There are a couple of spots within `copy_or_rename_section_in_file()` that incorrectly use an `int` to track an offset within a string, which may truncate or wrap around to a negative value. Historically it was impossible to have a line longer than 1024 bytes anyway, since we used fgets() with a fixed-size buffer of exactly that length. But the recent change to use a strbuf permits us to read lines of arbitrary length, so it's possible for a malicious input to cause us to overflow past INT_MAX and do an out-of-bounds array read. Practically speaking, however, this should never happen, since it requires 2GB section names or values, which are unrealistic in non-malicious circumstances. Co-authored-by: Jeff King <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent a5bb10f commit e91cfe6

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

config.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3027,9 +3027,10 @@ void git_config_set_multivar(const char *key, const char *value,
30273027
flags);
30283028
}
30293029

3030-
static int section_name_match (const char *buf, const char *name)
3030+
static size_t section_name_match (const char *buf, const char *name)
30313031
{
3032-
int i = 0, j = 0, dot = 0;
3032+
size_t i = 0, j = 0;
3033+
int dot = 0;
30333034
if (buf[i] != '[')
30343035
return 0;
30353036
for (i = 1; buf[i] && buf[i] != ']'; i++) {
@@ -3133,15 +3134,14 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
31333134
}
31343135

31353136
while (!strbuf_getwholeline(&buf, config_file, '\n')) {
3136-
unsigned i;
3137-
int length;
3137+
size_t i, length;
31383138
int is_section = 0;
31393139
char *output = buf.buf;
31403140
for (i = 0; buf.buf[i] && isspace(buf.buf[i]); i++)
31413141
; /* do nothing */
31423142
if (buf.buf[i] == '[') {
31433143
/* it's a section */
3144-
int offset;
3144+
size_t offset;
31453145
is_section = 1;
31463146

31473147
/*

0 commit comments

Comments
 (0)