From ff9092bc9f1ce8927ff7f7b7bdefc36071661f06 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sun, 17 Mar 2024 15:59:47 -0400 Subject: [PATCH] Fix slice bounds errors when editing notes on short entries --- src/cli/edit.go | 9 ++++++--- src/offline/edit.go | 8 ++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/cli/edit.go b/src/cli/edit.go index 28809f7..a9e46c4 100644 --- a/src/cli/edit.go +++ b/src/cli/edit.go @@ -30,9 +30,7 @@ func EditEntry(targetLocation string, hidePassword bool, field int) { unencryptedEntry := offline.DecryptGPG(targetLocation) // ensure slice is long enough for field - for len(unencryptedEntry) <= field { - unencryptedEntry = append(unencryptedEntry, "") - } + unencryptedEntry = offline.EnsureSliceLength(unencryptedEntry, field) // edit the field switch field { @@ -48,6 +46,7 @@ func EditEntry(targetLocation string, hidePassword bool, field int) { writeEntryShortcut(targetLocation, unencryptedEntry, hidePassword) } +// EditEntryNote edits the note of an entry at targetLocation (user input) func EditEntryNote(targetLocation string, hidePassword bool) { // ensure targetLocation exists offline.TargetIsFile(targetLocation, true, 2) @@ -55,6 +54,10 @@ func EditEntryNote(targetLocation string, hidePassword bool) { // read old entry data unencryptedEntry := offline.DecryptGPG(targetLocation) + // ensure slice is long enough for note + // avoids errors when storing non-note data + unencryptedEntry = offline.EnsureSliceLength(unencryptedEntry, 2) // 2 is used because it is the index of URL, the last non-note field + // store non-note data separately nonNoteData := unencryptedEntry[:3] diff --git a/src/offline/edit.go b/src/offline/edit.go index 8b66979..5242394 100644 --- a/src/offline/edit.go +++ b/src/offline/edit.go @@ -23,3 +23,11 @@ func Rename(oldLocation string, newLocation string) { // TODO If in online mode, check if oldLocation is a directory and rename it on the server os.Exit(0) } + +// EnsureSliceLength ensures slice is long enough to contain the specified index +func EnsureSliceLength(slice []string, index int) []string { + for len(slice) <= index { + slice = append(slice, "") + } + return slice +}