Signed-off-by: Derek Collison <derek@nats.io>
This commit is contained in:
Derek Collison
2020-12-13 11:49:48 -08:00
parent d5f255b98e
commit be6289be51
2 changed files with 21 additions and 0 deletions

View File

@@ -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

View File

@@ -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)
}
}