Skip to content

Commit 487af0c

Browse files
committed
index: correct index has_dir_name check
`has_dir_name` is used to check for directory/file collisions, and attempts to determine whether the index contains a file with a directory name that is a proper subset of the new index entry that we're trying to add. To determine directory name, the function would walk the path string backwards to identify a `/`, stopping at the end of the string. However, the function assumed that the strings did not start with a `/`. If the paths contain only a single `/` at the beginning of the string, then the function would continue the loop, erroneously, when they should have stopped at the first character. Correct the order of the tests to terminate properly. Credit to Michael Rodler (@f0rki) and Amazon AWS Security.
1 parent 19f3dcd commit 487af0c

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/libgit2/index.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,10 +1185,13 @@ static int has_dir_name(git_index *index,
11851185
size_t len, pos;
11861186

11871187
for (;;) {
1188-
if (*--slash == '/')
1189-
break;
1188+
slash--;
1189+
11901190
if (slash <= entry->path)
11911191
return 0;
1192+
1193+
if (*slash == '/')
1194+
break;
11921195
}
11931196
len = slash - name;
11941197

0 commit comments

Comments
 (0)