Fix slice bounds errors when editing notes on short entries

This commit is contained in:
2024-03-17 15:59:47 -04:00
parent 279384ee4a
commit ff9092bc9f
2 changed files with 14 additions and 3 deletions
+6 -3
View File
@@ -30,9 +30,7 @@ func EditEntry(targetLocation string, hidePassword bool, field int) {
unencryptedEntry := offline.DecryptGPG(targetLocation) unencryptedEntry := offline.DecryptGPG(targetLocation)
// ensure slice is long enough for field // ensure slice is long enough for field
for len(unencryptedEntry) <= field { unencryptedEntry = offline.EnsureSliceLength(unencryptedEntry, field)
unencryptedEntry = append(unencryptedEntry, "")
}
// edit the field // edit the field
switch field { switch field {
@@ -48,6 +46,7 @@ func EditEntry(targetLocation string, hidePassword bool, field int) {
writeEntryShortcut(targetLocation, unencryptedEntry, hidePassword) writeEntryShortcut(targetLocation, unencryptedEntry, hidePassword)
} }
// EditEntryNote edits the note of an entry at targetLocation (user input)
func EditEntryNote(targetLocation string, hidePassword bool) { func EditEntryNote(targetLocation string, hidePassword bool) {
// ensure targetLocation exists // ensure targetLocation exists
offline.TargetIsFile(targetLocation, true, 2) offline.TargetIsFile(targetLocation, true, 2)
@@ -55,6 +54,10 @@ func EditEntryNote(targetLocation string, hidePassword bool) {
// read old entry data // read old entry data
unencryptedEntry := offline.DecryptGPG(targetLocation) 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 // store non-note data separately
nonNoteData := unencryptedEntry[:3] nonNoteData := unencryptedEntry[:3]
+8
View File
@@ -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 // TODO If in online mode, check if oldLocation is a directory and rename it on the server
os.Exit(0) 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
}