{"record":{"id":"777e2cf69cddfeef","repo":"tursodatabase/turso","slug":"an-attempt-was-made-to-seek-before-the-beginning-o","errorCode":null,"errorMessage":"An attempt was made to seek before the beginning of the stream.","messagePattern":"An attempt was made to seek before the beginning of the stream\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data.Sqlite/SqliteBlob.cs","lineNumber":80,"sourceCode":"\n    public override int Read(byte[] buffer, int offset, int count)\n    {\n        ValidateBuffer(buffer, offset, count);\n        return GetStream().Read(buffer, offset, count);\n    }\n\n    public override long Seek(long offset, SeekOrigin origin)\n    {\n        var stream = GetStream();\n        var position = origin switch\n        {\n            SeekOrigin.Begin => offset,\n            SeekOrigin.Current => stream.Position + offset,\n            SeekOrigin.End => stream.Length + offset,\n            _ => throw new ArgumentException(Properties.Resources.InvalidEnumValue(typeof(SeekOrigin), origin), nameof(origin))\n        };\n        if (position < 0)\n            throw new IOException(Properties.Resources.SeekBeforeBegin);\n\n        stream.Position = position;\n        return position;\n    }\n\n    public override void SetLength(long value)\n    {\n        throw new NotSupportedException(Properties.Resources.ResizeNotSupported);\n    }\n\n    public override void Write(byte[] buffer, int offset, int count)\n    {\n        if (_readOnly)\n            throw new NotSupportedException(Properties.Resources.WriteNotSupported);\n\n        ValidateBuffer(buffer, offset, count);\n        if (count == 0)\n            return;","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/tursodatabase/turso/blob/244cde92a7df7f9b8b8b7a4075c35a12977e303e/bindings/dotnet/src/Turso.Data.Sqlite/SqliteBlob.cs#L62-L98","documentation":"After resolving the target position (offset from Begin/Current/End), SqliteBlob.Seek requires the result to be non-negative; a negative position is impossible for the underlying MemoryStream, so it throws IOException (Properties.Resources.SeekBeforeBegin), matching System.IO stream semantics: seeking before the start is an I/O-level error, not an argument error.","triggerScenarios":"Seek(-n, SeekOrigin.Current) with n greater than the current Position; Seek(-n, SeekOrigin.End) with n greater than the blob length; passing a negative offset with SeekOrigin.Begin.","commonSituations":"Generic stream helpers that rewind by struct sizes (e.g., Seek(-sizeof(Header), Current)) after fewer bytes were actually read; parsers whose offset math was tuned to a different file layout; reuse of relative-seek code on short blobs.","solutions":["Compute the target position yourself first and handle the negative case before calling Seek.","For rewinds, prefer an absolute Seek(x, SeekOrigin.Begin) over large negative relative offsets.","Base rewind amounts on bytes actually read (stream.Position), not on assumed layout sizes."],"exampleFix":"// before\nblob.Seek(-16, SeekOrigin.Current); // fewer than 16 bytes were read -> negative position\n\n// after\nlong rewind = Math.Min(16, blob.Position);\nblob.Seek(-rewind, SeekOrigin.Current);","handlingStrategy":"validation","validationCode":"long target = origin switch {\n    SeekOrigin.Begin => offset,\n    SeekOrigin.Current => blob.Position + offset,\n    SeekOrigin.End => blob.Length + offset,\n    _ => throw new ArgumentOutOfRangeException(nameof(origin))\n};\nif (target < 0) throw new IOException(\"seek before begin\");\nblob.Seek(offset, origin);","typeGuard":null,"tryCatchPattern":"try {\n    blob.Seek(offset, origin);\n} catch (IOException) when (origin != SeekOrigin.Begin && offset < 0) {\n    // record shorter than the fixed header expects: treat as malformed\n}","preventionTips":["Derive rewind amounts from bytes actually read, never from struct sizes.","Use absolute Begin-based seeks for fixed-layout records.","Validate relative-seek math against Position/Length before calling Seek."],"tags":["dotnet","stream","seek","io"],"backgroundTag":"stream-seek-out-of-range","analyzedSha":"244cde92a7df7f9b8b8b7a4075c35a12977e303e","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}