Skip to content
Merged
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
9 changes: 2 additions & 7 deletions internal/codeguard/runner/support/rule_stats_history.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package support

import (
"encoding/json"
"os"
"strings"
"time"

"github.com/devr-tools/codeguard/internal/codeguard/cachefile"
"github.com/devr-tools/codeguard/internal/codeguard/core"
)

Expand All @@ -32,12 +31,8 @@ func LoadRuleStatsHistory(path string) []core.RuleStatsHistoryEntry {
if strings.TrimSpace(path) == "" {
return nil
}
data, err := os.ReadFile(path) //nolint:gosec // config-supplied rule-stats history cache path
if err != nil {
return nil
}
var file ruleStatsHistoryFile
if err := json.Unmarshal(data, &file); err != nil || file.Version != ruleStatsHistoryVersion {
if !cachefile.Load(path, &file) || file.Version != ruleStatsHistoryVersion {
return nil
}
return file.Entries
Expand Down
20 changes: 20 additions & 0 deletions tests/support/rule_stats_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package support_test

import (
"fmt"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -29,6 +30,25 @@ func TestRuleStatsHistoryPathForBase(t *testing.T) {
}
}

func TestLoadRuleStatsHistoryRejectsOversizedFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "cache.rule-stats-history.json")
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
if err := f.Truncate((32 << 20) + 1); err != nil {
_ = f.Close()
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}

if got := runnersupport.LoadRuleStatsHistory(path); len(got) != 0 {
t.Fatalf("expected empty history for oversized file, got %#v", got)
}
}

func TestRuleStatsHistoryRoundTripAndCap(t *testing.T) {
path := filepath.Join(t.TempDir(), "cache.rule-stats-history.json")

Expand Down
Loading