{"record":{"id":"39ec62d2a0f043f5","repo":"nats-io/nats-server","slug":"invalid-message-lengths-for-sequence-d","errorCode":null,"errorMessage":"invalid message lengths for sequence %d","messagePattern":"invalid message lengths for sequence (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/stream_backup.go","lineNumber":475,"sourceCode":"\tlseq := nstate.FirstSeq - 1\n\teob := false\n\tmp := int64(s.getOpts().MaxPayload)\n\tfor {\n\t\thdr, err := tr.Next()\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tseq := hdr.Sequence\n\t\tif seq == 0 {\n\t\t\t// Sentinel \"end of backup\" if all fields are zero.\n\t\t\tif hdr.Timestamp == 0 && hdr.HeaderSize == 0 && hdr.PayloadSize == 0 {\n\t\t\t\teob = true\n\t\t\t\tbreak\n\t\t\t}\n\t\t\treturn nil, fmt.Errorf(\"expected message sequence\")\n\t\t}\n\t\tif hdr.HeaderSize < 0 || hdr.PayloadSize < 0 || hdr.PayloadSize > math.MaxInt64-hdr.HeaderSize {\n\t\t\treturn nil, fmt.Errorf(\"invalid message lengths for sequence %d\", seq)\n\t\t}\n\t\tdeclaredSize := hdr.HeaderSize + hdr.PayloadSize\n\t\tif hdr.HeaderSize > mp || hdr.PayloadSize > mp-hdr.HeaderSize {\n\t\t\treturn nil, fmt.Errorf(\"message sequence %d exceeds maximum payload size\", seq)\n\t\t}\n\t\tif mms := int64(cfg.MaxMsgSize); mms >= 0 && (hdr.HeaderSize > mms || hdr.PayloadSize > mms-hdr.HeaderSize) {\n\t\t\treturn nil, fmt.Errorf(\"message sequence %d exceeds maximum message size\", seq)\n\t\t}\n\t\tvar storedSizeRaw uint64\n\t\tswitch cfg.Storage {\n\t\tcase MemoryStorage:\n\t\t\tstoredSizeRaw = memStoreMsgSizeRaw(len(hdr.Name), int(hdr.HeaderSize), int(hdr.PayloadSize))\n\t\tdefault:\n\t\t\tstoredSizeRaw = fileStoreMsgSizeRaw(len(hdr.Name), int(hdr.HeaderSize), int(hdr.PayloadSize))\n\t\t}\n\t\tif storedSizeRaw > math.MaxInt64 {\n\t\t\treturn nil, fmt.Errorf(\"snapshot message bytes exceed reserved restore size\")\n\t\t}","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/stream_backup.go#L457-L493","documentation":"Validation guard on the restore message header: HeaderSize or PayloadSize is negative, or their sum overflows int64. Such lengths cannot occur in a well-formed archive and indicate header corruption or an archive produced/modified by incompatible tooling; the check runs before any allocation based on these sizes.","triggerScenarios":"Thrown at server/stream_backup.go:475 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Regenerate the backup and verify it was not modified or truncated in transit","Use matching server versions for backup and restore","If importing third-party archives, validate header fields before feeding the tar stream to restore"],"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-08T15:18:49.778Z"}