{"record":{"id":"89947791f7ffe6dc","repo":"microsoft/FASTER","slug":"byte-array-provided-has-invalid-length","errorCode":null,"errorMessage":"Byte array provided has invalid length","messagePattern":"Byte array provided has invalid length","errorType":"exception","errorClass":"FasterException","httpStatus":null,"severity":"error","filePath":"cs/src/core/FasterLog/FasterLogIterator.cs","lineNumber":302,"sourceCode":"                    FasterLogRecoveryInfo info = new();\n                    info.Initialize(new BinaryReader(new UnmanagedMemoryStream((byte*) (headerSize + physicalAddress), entryLength)));\n                    if (info.CommitNum != long.MaxValue) continue;\n                    \n                    // Otherwise, no more entries\n                    entry = default;\n                    entryLength = default;\n                    epoch.Suspend();\n                    return false;\n                }\n                \n                if (getMemory != null)\n                {\n                    // Use user delegate to allocate memory\n                    entry = getMemory(entryLength);\n                    if (entry.Length < entryLength)\n                    {\n                        epoch.Suspend();\n                        throw new FasterException(\"Byte array provided has invalid length\");\n                    }\n                }\n                else\n                {\n                    // We allocate a byte array from heap\n                    entry = new byte[entryLength];\n                }\n\n                fixed (byte* bp = entry)\n                    Buffer.MemoryCopy((void*) (headerSize + physicalAddress), bp, entryLength, entryLength);\n                \n                epoch.Suspend();\n                return true;\n            }\n        }\n\n        /// <summary>\n        /// GetNext supporting memory pools","sourceCodeStart":284,"sourceCodeEnd":320,"githubUrl":"https://github.com/microsoft/FASTER/blob/321d872eabda6a0345c8bd76419f89723ed864ae/cs/src/core/FasterLog/FasterLogIterator.cs#L284-L320","documentation":"When iterating, the caller supplied a getMemory allocator delegate whose returned buffer is shorter than the record length being read. The iterator (FasterLogIterator.cs:302, reachable from GetNext/GetAsyncEnumerable) suspends the epoch and throws because the record would be truncated.","triggerScenarios":"Passing an allocator Func<int, byte[]>/IMemoryOwner factory that returns pooled or pre-sized buffers smaller than the record length; iterating records written with a larger payload schema than the current allocator expects.","commonSituations":"Using a fixed-size buffer pool without honoring the requested length argument; schema/version change where new larger records are read by old reader code; pooling bugs returning zero-length arrays.","solutions":["Make the getMemory delegate honor the requested length: allocate at least entryLength bytes or grow/retrip the buffer.","Use ArrayPool with exact rentals: ArrayPool<byte>.Shared.Rent(entryLength) and handle the oversize array, or verify Length >= entryLength.","Remove fixed-size pre-allocated buffers from the allocator path.","Fall back to the default heap allocation by not passing a custom allocator."],"exampleFix":"// before\nmemory => fixedBuffer // Length may be < entryLength\n\n// after\nmemory => ArrayPool<byte>.Shared.Rent(entryLength) // always >= entryLength","handlingStrategy":"validation","validationCode":"byte[] GetMemory(int length) => length <= maxSupportedRecordSize ? ArrayPool<byte>.Shared.Rent(length) : throw new ArgumentException($\"Record length {length} exceeds supported size\");","typeGuard":null,"tryCatchPattern":"try { iter.GetNext(out var entry, out var addr); } catch (FasterException ex) when (ex.Message == \"Byte array provided has invalid length\") { /* fix allocator delegate */ }","preventionTips":["Allocator must honor the requested length","Pool rentals must be >= requested size","Test the allocator with the largest record the schema allows"],"tags":["fasterlog","iterator","buffer-size","argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"321d872eabda6a0345c8bd76419f89723ed864ae","analyzedAt":"2026-09-15T22:18:00.693Z","contentChangedAt":"2026-09-15T22:18:00.693Z","schemaVersion":2},"datasetVersion":"2026-09-23T02:17:17.105Z"}