From 4a5cbf2a19ebf0489bbc6654f7e0972cffa23440 Mon Sep 17 00:00:00 2001 From: buengese Date: Wed, 14 Apr 2021 18:20:17 +0200 Subject: [PATCH] cmd/ncdu: fix out of range panic in delete --- cmd/ncdu/ncdu.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/ncdu/ncdu.go b/cmd/ncdu/ncdu.go index 8b95c9b8e..49e12a887 100644 --- a/cmd/ncdu/ncdu.go +++ b/cmd/ncdu/ncdu.go @@ -485,11 +485,15 @@ func (u *UI) removeEntry(pos int) { // delete the entry at the current position func (u *UI) delete() { + if u.d == nil || len(u.entries) == 0 { + return + } ctx := context.Background() - dirPos := u.sortPerm[u.dirPosMap[u.path].entry] - entry := u.entries[dirPos] + cursorPos := u.dirPosMap[u.path] + dirPos := u.sortPerm[cursorPos.entry] + dirEntry := u.entries[dirPos] u.boxMenu = []string{"cancel", "confirm"} - if obj, isFile := entry.(fs.Object); isFile { + if obj, isFile := dirEntry.(fs.Object); isFile { u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) { if o != 1 { return "Aborted!", nil @@ -499,27 +503,33 @@ func (u *UI) delete() { return "", err } u.removeEntry(dirPos) + if cursorPos.entry >= len(u.entries) { + u.move(-1) // move back onto a valid entry + } return "Successfully deleted file!", nil } u.popupBox([]string{ "Delete this file?", - u.fsName + entry.String()}) + u.fsName + dirEntry.String()}) } else { u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) { if o != 1 { return "Aborted!", nil } - err := operations.Purge(ctx, f, entry.String()) + err := operations.Purge(ctx, f, dirEntry.String()) if err != nil { return "", err } u.removeEntry(dirPos) + if cursorPos.entry >= len(u.entries) { + u.move(-1) // move back onto a valid entry + } return "Successfully purged folder!", nil } u.popupBox([]string{ "Purge this directory?", "ALL files in it will be deleted", - u.fsName + entry.String()}) + u.fsName + dirEntry.String()}) } }