ncdu: fix crash when re-entering changed directory after rescan

ncdu stores the position that it was in for each directory. However
doing a rescan can cause those positions to be out of range if the
number of files decreased in a directory. When re-entering the
directory, this causes an index out of range error.

This fixes the problem by detecting the index out of range and
flushing the saved directory position.

See: https://forum.rclone.org/t/slice-bounds-out-of-range-during-ncdu/42492/
This commit is contained in:
Nick Craig-Wood 2023-10-24 14:21:11 +01:00
parent e1ad467009
commit 53868ef4e1
1 changed files with 6 additions and 0 deletions

View File

@ -386,6 +386,12 @@ func (u *UI) Draw() {
}
showEmptyDir := u.hasEmptyDir()
dirPos := u.dirPosMap[u.path]
// Check to see if a rescan has invalidated the position
if dirPos.offset >= len(u.sortPerm) {
delete(u.dirPosMap, u.path)
dirPos.offset = 0
dirPos.entry = 0
}
for i, j := range u.sortPerm[dirPos.offset:] {
entry := u.entries[j]
n := i + dirPos.offset