nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: nats-io/nats-server
Pull request #1775 (2020-12-13 19:54:09)

Fix for https://github.com/nats-io/jetstream/issues/406
Signed-off-by: Derek Collison <derek@nats.io>

Resolves #https://github.com/nats-io/jetstream/issues/406

/cc @nats-io/core



Patch:
diff --git a/server/filestore.go b/server/filestore.go
index da13e9ee..1aaf65a8 100644
--- a/server/filestore.go
+++ b/server/filestore.go
@@ -2289,6 +2289,12 @@ func (mb *msgBlock) readIndexInfo() error {
 	mb.last.ts = readTimeStamp()
 	dmapLen := readCount()
 
+	// Check if this is a short write index file.
+	if bi < 0 || bi+checksumSize > len(buf) {
+		defer os.Remove(mb.ifn)
+		return fmt.Errorf("short index file")
+	}
+
 	// Checksum
 	copy(mb.lchk[0:], buf[bi:bi+checksumSize])
 	bi += checksumSize


Test patch:
diff --git a/server/filestore_test.go b/server/filestore_test.go
index 5e431718..956fe18d 100644
--- a/server/filestore_test.go
+++ b/server/filestore_test.go
@@ -17,6 +17,7 @@ import (
 	"archive/tar"
 	"bytes"
 	"compress/gzip"
+	"encoding/base64"
 	"encoding/hex"
 	"encoding/json"
 	"fmt"
@@ -28,6 +29,7 @@ import (
 	"path"
 	"path/filepath"
 	"reflect"
+	"strings"
 	"testing"
 	"time"
 )
@@ -2410,3 +2412,16 @@ func TestFileStoreConsumerPerf(t *testing.T) {
 	oc.syncStateFile()
 	fmt.Printf("time to sync is %v\n", time.Since(start))
 }
+
+func TestFileStoreStreamIndexBug(t *testing.T) {
+	// https://github.com/nats-io/jetstream/issues/406
+	badIdxBytes, _ := base64.StdEncoding.DecodeString("FgGBkw7D/f8/772iDPDIgbU=")
+	dir, _ := ioutil.TempDir("", "js-bad-idx-")
+	defer os.RemoveAll(dir)
+	fn := path.Join(dir, "1.idx")
+	ioutil.WriteFile(fn, badIdxBytes, 0644)
+	mb := &msgBlock{index: 1, ifn: fn}
+	if err := mb.readIndexInfo(); err == nil || !strings.Contains(err.Error(), "short index") {
+		t.Fatalf("Expected error during readIndexInfo(): %v", err)
+	}
+}