{"record":{"id":"72c79db2467cfd22","repo":"nats-io/nats-server","slug":"indexcachebuf-corrupt-state-in-s-mb-first-d-mb","errorCode":null,"errorMessage":"indexCacheBuf corrupt state in %s: mb.first %d mb.last %d","messagePattern":"indexCacheBuf corrupt state in (.+?): mb\\.first (.+?) mb\\.last (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/filestore.go","lineNumber":8457,"sourceCode":"\t}\n\treturn nil\n}\n\n// Index a raw msg buffer.\n// Lock should be held.\nfunc (mb *msgBlock) indexCacheBuf(buf []byte) error {\n\tvar le = binary.LittleEndian\n\n\tvar alloc bool\n\tvar idx []uint32\n\tvar index uint32\n\n\tmbFirstSeq := atomic.LoadUint64(&mb.first.seq)\n\tmbLastSeq := atomic.LoadUint64(&mb.last.seq)\n\n\t// Sanity check here since we calculate size to allocate based on this.\n\tif mbFirstSeq > (mbLastSeq + 1) { // Purged state first == last + 1\n\t\tmb.fs.warn(\"indexCacheBuf corrupt state in %s: mb.first %d mb.last %d\", mb.mfn, mbFirstSeq, mbLastSeq)\n\t\t// This would cause idxSz to wrap.\n\t\treturn errCorruptState\n\t}\n\n\tidxSz := mbLastSeq - mbFirstSeq + 1\n\n\tif mb.cache == nil {\n\t\tmb.cache = mb.ecache.Value()\n\t}\n\tif mb.cache == nil {\n\t\tmb.cache = &cache{}\n\t\talloc = true\n\t} else {\n\t\t// The buf arg already came from the pool probably, so there's\n\t\t// no point in reusing mb.cache.buf's underlying capacity here.\n\t\t// Just recycle it for the next block load.\n\t\tmb.cache.stopRecycle()\n\t\trecycleMsgBlockBuf(mb.cache.buf)","sourceCodeStart":8439,"sourceCodeEnd":8475,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/filestore.go#L8439-L8475","documentation":"While indexing a raw message-block buffer into the cache, mb.first was greater than mb.last+1 (only legal purged state is first == last+1). This would make the computed index size wrap, so indexCacheBuf aborts with errCorruptState instead of allocating a bogus buffer.","triggerScenarios":"Thrown at server/filestore.go:8457 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Treat the block as corrupt: let recovery rebuild the block or report lost data","Check for disk corruption or partial writes to the msg block file","Restore the block from backup if the data is critical"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}