Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions pkg/history/history.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package history

import (
"bufio"
"encoding/json"
"io"
"os"
"path/filepath"
"slices"
Expand Down Expand Up @@ -215,24 +215,18 @@ func (h *History) load() error {
defer f.Close()

var all []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}

dec := json.NewDecoder(f)
for {
var message string
if err := json.Unmarshal([]byte(line), &message); err != nil {
if err := dec.Decode(&message); err != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silent error suppression when decoding malformed JSON entries

When json.NewDecoder.Decode() encounters a non-EOF error (e.g., malformed JSON from file corruption or incomplete writes), the code silently continues with continue, skipping that line but proceeding to decode subsequent lines.

This differs from the previous bufio.Scanner behavior where scanner.Err() would return the error after the loop. While this makes the code more tolerant of corrupt lines, it could mask serious issues like file truncation or encoding problems. A corrupted history file could lead to silently losing valid entries that appear after a corrupt line, with no indication to the user that data was skipped.

Suggestion: Consider logging skipped entries or maintaining an error count:

if err := dec.Decode(&message); err != nil {
    if err == io.EOF {
        break
    }
    // Log or track decode errors
    slog.Warn("skipping malformed history entry", "error", err)
    continue
}

if err == io.EOF {
break
}
continue
}
all = append(all, message)
}

if err := scanner.Err(); err != nil {
return err
}

// Deduplicate keeping the latest occurrence of each message
seen := make(map[string]bool)
for i := len(all) - 1; i >= 0; i-- {
Expand Down
25 changes: 25 additions & 0 deletions pkg/history/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,28 @@ func TestHistory_SetCurrent(t *testing.T) {
h.SetCurrent(2)
assert.Empty(t, h.Next())
}

func TestHistory_VeryLongMessage(t *testing.T) {
tmpDir := t.TempDir()

h, err := New(WithBaseDir(tmpDir))
require.NoError(t, err)

// Create a message longer than bufio.Scanner's default 64KB limit
longMessage := make([]byte, 100*1024) // 100KB
for i := range longMessage {
longMessage[i] = 'a' + byte(i%26)
}
longStr := string(longMessage)

require.NoError(t, h.Add(longStr))
require.NoError(t, h.Add("short message after"))

// Reload history from disk
h2, err := New(WithBaseDir(tmpDir))
require.NoError(t, err)

require.Len(t, h2.Messages, 2)
assert.Equal(t, longStr, h2.Messages[0])
assert.Equal(t, "short message after", h2.Messages[1])
}