{"record":{"id":"ef94e4cd355f77a9","repo":"microsoft/FASTER","slug":"cannot-create-new-spanbyteandmemory-using-serialized","errorCode":null,"errorMessage":"Cannot create new SpanByteAndMemory using serialized SpanByte","messagePattern":"Cannot create new SpanByteAndMemory using serialized SpanByte","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"cs/src/core/VarLen/SpanByteAndMemory.cs","lineNumber":30,"sourceCode":"    public unsafe struct SpanByteAndMemory : IHeapConvertible\n    {\n        /// <summary>\n        /// Stack output as SpanByte\n        /// </summary>\n        public SpanByte SpanByte;\n\n        /// <summary>\n        /// Heap output as IMemoryOwner\n        /// </summary>\n        public IMemoryOwner<byte> Memory;\n\n        /// <summary>\n        /// Constructor using given SpanByte\n        /// </summary>\n        /// <param name=\"spanByte\"></param>\n        public SpanByteAndMemory(SpanByte spanByte)\n        {\n            if (spanByte.Serialized) throw new Exception(\"Cannot create new SpanByteAndMemory using serialized SpanByte\");\n            SpanByte = spanByte;\n            Memory = default;\n        }\n\n        /// <summary>\n        /// Constructor using SpanByte at given (fixed) pointer, of given length\n        /// </summary>\n        public SpanByteAndMemory(void* pointer, int length)\n        {\n            SpanByte = new SpanByte(length, (IntPtr)pointer);\n            Memory = default;\n        }\n\n        /// <summary>\n        /// Get length\n        /// </summary>\n        public int Length\n        {","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/microsoft/FASTER/blob/321d872eabda6a0345c8bd76419f89723ed864ae/cs/src/core/VarLen/SpanByteAndMemory.cs#L12-L48","documentation":"Exception thrown by the SpanByteAndMemory(SpanByte) constructor when the given SpanByte is marked Serialized. A serialized SpanByte wraps externally-owned storage (its payload lives behind a length-prefixed pointer), so it cannot be wrapped into a SpanByteAndMemory whose memory-state transitions assume an in-memory, unserialized payload.","triggerScenarios":"Passing a SpanByte obtained from a serialized record/log (spanByte.Serialized == true) into the SpanByteAndMemory(SpanByte) constructor — e.g. reading a value from the log and directly wrapping it for an output collector.","commonSituations":"Read-modify-write sessions reusing a record's SpanByte as an output; copying values out of tail/log records into IVariableLengthStruct output buffers; upgrading FASTER versions where serialization flags differ.","solutions":["Materialize the serialized SpanByte into a copy (e.g. SpanByteAndMemory.FromMemorySpan or copy payload to a new buffer) before wrapping","Use the pointer/length constructor with deserialized bytes instead of the SpanByte overload","Check spanByte.Serialized before constructing and handle the serialized case separately"],"exampleFix":"// before\nvar sb = input.Serialized ? input : input; // input is a serialized record SpanByte\nvar output = new SpanByteAndMemory(sb); // throws\n// after\nvar mem = MemoryPool<byte>.Rent(input.Length);\ninput.AsSpan().CopyTo(mem.Span);\nvar output = SpanByteAndMemory.FromMemorySpan(mem); // or use deserialized SpanByte","handlingStrategy":"validation","validationCode":"SpanByteAndMemory MakeOutput(SpanByte sb) => sb.Serialized ? CopyToNewMemory(sb) : new SpanByteAndMemory(sb);","typeGuard":"bool IsWrappable(SpanByte sb) => !sb.Serialized;","tryCatchPattern":"try { return new SpanByteAndMemory(spanByte); }\ncatch (Exception ex) when (ex.Message.Contains(\"serialized SpanByte\"))\n{ return SpanByteAndMemory.FromMemorySpan(CopyPayload(spanByte)); }","preventionTips":["Never wrap SpanBytes read directly from serialized log records","Copy payloads out of records before constructing outputs","Assert !spanByte.Serialized in debug builds before construction"],"tags":["spanbyte","memory","invalid-state","csharp"],"backgroundTag":"invalid-state-transition","analyzedSha":"321d872eabda6a0345c8bd76419f89723ed864ae","analyzedAt":"2026-09-15T22:18:00.693Z","contentChangedAt":"2026-09-15T22:18:00.693Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}