{"record":{"id":"94e0993efebd8d67","repo":"antlr/antlr4","slug":"seek-to-index-outside-buffer-index-not-in-bu","errorCode":null,"errorMessage":"seek to index outside buffer: ${index} not in ${bufferStartIndex}..${bufferStartIndex+n}","messagePattern":"seek to index outside buffer: (.+?) not in (.+?)\\.\\.(.+?)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/UnbufferedCharStream.cs","lineNumber":391,"sourceCode":"            {\n                return;\n            }\n            if (index > currentCharIndex)\n            {\n                Sync(index - currentCharIndex);\n                index = Math.Min(index, BufferStartIndex + n - 1);\n            }\n            // index == to bufferStartIndex should set p to 0\n            int i = index - BufferStartIndex;\n            if (i < 0)\n            {\n                throw new ArgumentException(\"cannot seek to negative index \" + index);\n            }\n            else\n            {\n                if (i >= n)\n                {\n                    throw new NotSupportedException(\"seek to index outside buffer: \" + index + \" not in \" + BufferStartIndex + \"..\" + (BufferStartIndex + n));\n                }\n            }\n            p = i;\n            currentCharIndex = index;\n            if (p == 0)\n            {\n                lastChar = lastCharBufferStart;\n            }\n            else\n            {\n                lastChar = data[p - 1];\n            }\n        }\n\n        public virtual int Size\n        {\n            get\n            {","sourceCodeStart":373,"sourceCodeEnd":409,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/UnbufferedCharStream.cs#L373-L409","documentation":"Seek() first tries to synchronize the requested position into the rolling buffer. If the requested absolute index still maps beyond the end of that buffer, the stream cannot provide that position and throws NotSupportedException. This is distinct from the negative-index case, which indicates discarded data behind the stream.","triggerScenarios":"Calling Seek() with an index far ahead of what has been read; seeking to an index beyond available input/EOF under buffering constraints; or custom code assuming the unbuffered stream can jump arbitrarily far forward.","commonSituations":"Absolute-index navigation from buffered token-stream code is reused on UnbufferedCharStream; incorrect char-index arithmetic after multi-byte/surrogate handling; or attempts to seek to a computed EOF position.","solutions":["Avoid long forward seeks; consume or Sync incrementally instead.","Check index calculations against the actual stream position before seeking.","Use AntlrInputStream when jump-to-index access is required."],"exampleFix":"// before\nchars.Seek(targetIndex); // target may not be synchronizable\n\n// after\nif (targetIndex > chars.Index)\n    while (chars.Index < targetIndex && chars.LA(1) != IntStreamConstants.EOF)\n        chars.Consume();\nchars.Seek(Math.Min(targetIndex, chars.Index));","handlingStrategy":"validation","validationCode":"if (targetIndex > chars.Index) {\n    while (chars.Index < targetIndex && chars.LA(1) != IntStreamConstants.EOF)\n        chars.Consume();\n}\nchars.Seek(Math.Min(targetIndex, chars.Index));","typeGuard":null,"tryCatchPattern":"try { chars.Seek(index); }\ncatch (NotSupportedException ex) when (ex.Message.StartsWith(\"seek to index outside buffer\")) { /* consume incrementally or use AntlrInputStream */ }","preventionTips":["Do not jump far ahead on unbuffered streams.","Advance with Consume()/Sync-style reads when forward progress is needed.","Use fully buffered input for absolute-position navigation."],"tags":["antlr","csharp","char-stream","seek","streaming"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}