Skip to content

Commit 04ff800

Browse files
pks-tgitster
authored andcommitted
dir: fix off by one errors for ignored and untracked entries
In `treat_directory()` we perform some logic to handle ignored and untracked entries. When populating a directory with entries we first save the current number of ignored/untracked entries and then populate new entries at the end of our arrays that keep track of those entries. When we figure out that all entries have been ignored/are untracked we then remove this tail of entries from those vectors again. But there is an off by one error in both paths that causes us to not free the first ignored and untracked entries, respectively. Fix these off-by-one errors to plug the resulting leak. While at it, massage the code a bit to match our modern code style. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5bf922a commit 04ff800

File tree

4 files changed

+5
-4
lines changed

4 files changed

+5
-4
lines changed

dir.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,8 +2135,7 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
21352135
*/
21362136
state = path_none;
21372137
} else {
2138-
int i;
2139-
for (i = old_ignored_nr + 1; i<dir->ignored_nr; ++i)
2138+
for (int i = old_ignored_nr; i < dir->ignored_nr; i++)
21402139
FREE_AND_NULL(dir->ignored[i]);
21412140
dir->ignored_nr = old_ignored_nr;
21422141
}
@@ -2148,8 +2147,7 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
21482147
*/
21492148
if ((dir->flags & DIR_SHOW_IGNORED_TOO) &&
21502149
!(dir->flags & DIR_KEEP_UNTRACKED_CONTENTS)) {
2151-
int i;
2152-
for (i = old_untracked_nr + 1; i<dir->nr; ++i)
2150+
for (int i = old_untracked_nr; i < dir->nr; i++)
21532151
FREE_AND_NULL(dir->entries[i]);
21542152
dir->nr = old_untracked_nr;
21552153
}

t/t3011-common-prefixes-and-directory-traversal.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='directory traversal handling, especially with common prefixes'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
test_expect_success 'setup' '

t/t7061-wtstatus-ignore.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='git-status ignored files'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
cat >expected <<\EOF

t/t7521-ignored-mode.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='git status ignored modes'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
test_expect_success 'setup initial commit and ignore file' '

0 commit comments

Comments
 (0)