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
8 changes: 8 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ func loadMetadata(dir string) (uint64, error) {
if maxBucketChunks == 0 {
return 0, fmt.Errorf("invalid maxBucketChunks=0 read from %q", metadataPath)
}
maxAllowedChunks := maxBucketSize / chunkSize
if maxBucketChunks > maxAllowedChunks {
Copy link
Copy Markdown
Author

@cubic-dev-ai cubic-dev-ai Bot Apr 15, 2026

Choose a reason for hiding this comment

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

P1: loadMetadata allows an invalid boundary value (maxBucketChunks == maxBucketSize/chunkSize) that bucket.Load rejects later, after potential large allocation work. Reject this value early to keep validation consistent and avoid pre-failure memory pressure.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At file.go, line 232:

<comment>`loadMetadata` allows an invalid boundary value (`maxBucketChunks == maxBucketSize/chunkSize`) that `bucket.Load` rejects later, after potential large allocation work. Reject this value early to keep validation consistent and avoid pre-failure memory pressure.</comment>

<file context>
@@ -228,6 +228,10 @@ func loadMetadata(dir string) (uint64, error) {
 		return 0, fmt.Errorf("invalid maxBucketChunks=0 read from %q", metadataPath)
 	}
+	maxAllowedChunks := maxBucketSize / chunkSize
+	if maxBucketChunks > maxAllowedChunks {
+		return 0, fmt.Errorf("too big maxBucketChunks=%d read from %q; cannot exceed %d", maxBucketChunks, metadataPath, maxAllowedChunks)
+	}
</file context>
Suggested change
if maxBucketChunks > maxAllowedChunks {
if maxBucketChunks >= maxAllowedChunks {
Fix with Cubic

return 0, fmt.Errorf("too big maxBucketChunks=%d read from %q; cannot exceed %d", maxBucketChunks, metadataPath, maxAllowedChunks)
}
return maxBucketChunks, nil
}

Expand Down Expand Up @@ -352,6 +356,10 @@ func (b *bucket) Load(r io.Reader, maxChunks uint64) error {
if err != nil {
return fmt.Errorf("cannot read len(b.m): %s", err)
}
maxKvs := maxChunks * chunkSize / 4
if kvsLen > maxKvs {
return fmt.Errorf("too big kvsLen=%d; cannot exceed %d", kvsLen, maxKvs)
}
kvsLen *= 2 * 8
kvs := make([]byte, kvsLen)
if _, err := io.ReadFull(r, kvs); err != nil {
Expand Down
55 changes: 55 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package fastcache

import (
"encoding/binary"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"testing"
)
Expand Down Expand Up @@ -259,3 +261,56 @@ func TestSaveLoadConcurrent(t *testing.T) {
close(stopCh)
wgWorkers.Wait()
}

func writeUint64ToBytes(v uint64) []byte {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], v)
return buf[:]
}

func TestLoadCorruptedMetadataTooBigChunks(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)

filePath := filepath.Join(tmpDir, "corrupted.fastcache")
if err := os.MkdirAll(filePath, 0755); err != nil {
t.Fatal(err)
}

metadataPath := filepath.Join(filePath, "metadata.bin")
hugeChunks := maxBucketSize/chunkSize + 1
if err := os.WriteFile(metadataPath, writeUint64ToBytes(hugeChunks), 0644); err != nil {
t.Fatal(err)
}

_, err = LoadFromFile(filePath)
if err == nil {
t.Fatal("expected error for corrupted metadata with huge maxBucketChunks")
}
if !strings.Contains(err.Error(), "too big maxBucketChunks") {
t.Fatalf("unexpected error: %s", err)
}
}

func TestLoadCorruptedDataTooBigKvsLen(t *testing.T) {
Copy link
Copy Markdown
Author

@cubic-dev-ai cubic-dev-ai Bot Apr 15, 2026

Choose a reason for hiding this comment

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

P2: This test name claims corrupted kvsLen coverage, but the body only verifies a valid round-trip, so the new corruption bound is not actually tested.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At file_test.go, line 298:

<comment>This test name claims corrupted `kvsLen` coverage, but the body only verifies a valid round-trip, so the new corruption bound is not actually tested.</comment>

<file context>
@@ -259,3 +261,56 @@ func TestSaveLoadConcurrent(t *testing.T) {
+	}
+}
+
+func TestLoadCorruptedDataTooBigKvsLen(t *testing.T) {
+	tmpDir, err := ioutil.TempDir("", "test")
+	if err != nil {
</file context>
Suggested change
func TestLoadCorruptedDataTooBigKvsLen(t *testing.T) {
func TestLoadRoundTripWithValidation(t *testing.T) {
Fix with Cubic

tmpDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)

filePath := filepath.Join(tmpDir, "corrupted.fastcache")
c := New(bucketsCount * chunkSize * 2)
c.Set([]byte("key"), []byte("value"))
if err := c.SaveToFile(filePath); err != nil {
t.Fatalf("SaveToFile error: %s", err)
}

_, err = LoadFromFile(filePath)
if err != nil {
t.Fatalf("LoadFromFile must succeed for valid cache: %s", err)
}
}
Loading