Skip to content

Commit

Permalink
index: correct index has_dir_name check
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
ethomson committed Jan 12, 2024
1 parent 05cf155 commit eb4c171
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/libgit2/index.c
Expand Up @@ -1148,10 +1148,13 @@ static int has_dir_name(git_index *index,
size_t len, pos;

for (;;) {
if (*--slash == '/')
break;
slash--;

if (slash <= entry->path)
return 0;

if (*slash == '/')
break;
}
len = slash - name;

Expand Down

0 comments on commit eb4c171

Please sign in to comment.