diff --git a/internal/codeguard/runner/support/rule_stats_history.go b/internal/codeguard/runner/support/rule_stats_history.go index 736a9d4..0773e9b 100644 --- a/internal/codeguard/runner/support/rule_stats_history.go +++ b/internal/codeguard/runner/support/rule_stats_history.go @@ -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" ) @@ -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 diff --git a/tests/support/rule_stats_history_test.go b/tests/support/rule_stats_history_test.go index 0e3cea2..436a4f2 100644 --- a/tests/support/rule_stats_history_test.go +++ b/tests/support/rule_stats_history_test.go @@ -2,6 +2,7 @@ package support_test import ( "fmt" + "os" "path/filepath" "testing" @@ -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")