{"record":{"id":"c0f1f82a40205876","repo":"antlr/antlr4","slug":"the-interval-extends-past-the-end-of-the-stream-c0f1f8","errorCode":null,"errorMessage":"the interval extends past the end of the stream","messagePattern":"the interval extends past the end of the stream","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/UnbufferedCharStream.cs","lineNumber":437,"sourceCode":"                {\n                    return IntStreamConstants.UnknownSourceName;\n                }\n                return name;\n            }\n        }\n\n        public virtual string GetText(Interval interval)\n        {\n            if (interval.a < 0 || interval.b < interval.a - 1)\n            {\n                throw new ArgumentException(\"invalid interval\");\n            }\n            int bufferStartIndex = BufferStartIndex;\n            if (n > 0 && data[n - 1] == IntStreamConstants.EOF)\n            {\n                if (interval.a + interval.Length > bufferStartIndex + n)\n                {\n                    throw new ArgumentException(\"the interval extends past the end of the stream\");\n                }\n            }\n            if (interval.a < bufferStartIndex || interval.b >= bufferStartIndex + n)\n            {\n                throw new NotSupportedException(\"interval \" + interval + \" outside buffer: \" + bufferStartIndex + \"..\" + (bufferStartIndex + n - 1));\n            }\n            // convert from absolute to local index\n            int i = interval.a - bufferStartIndex;\n            // build a UTF-16 string from the Unicode code points in data\n            var sb = new StringBuilder(interval.Length);\n            for (int offset = 0; offset < interval.Length; offset++) {\n                sb.Append(Char.ConvertFromUtf32(data[i + offset]));\n            }\n            return sb.ToString();\n        }\n\n        protected internal int BufferStartIndex\n        {","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/UnbufferedCharStream.cs#L419-L455","documentation":"If UnbufferedCharStream's buffer already ends with EOF, GetText knows the true end of input. It rejects intervals whose start plus length extends beyond that end. In short, the requested text range reaches past EOF.","triggerScenarios":"Calling GetText with a stop index beyond EOF; passing an oversized interval such as Interval.Of(0, int.MaxValue); or using stale/overlarge token character indexes after recovery at EOF.","commonSituations":"Error-recovery tokens with invalid stop indexes; code that uses a sentinel end index; or generic range extraction copied from a fully buffered implementation.","solutions":["Clamp the stop index to the last valid character index before calling GetText.","Use the exact start/stop character indexes from tokens.","Avoid sentinel values such as int.MaxValue for ranges on unbuffered input."],"exampleFix":"// before\nstring text = chars.GetText(Interval.Of(start, int.MaxValue));\n\n// after\nint last = chars.Index - 1; // valid once EOF has been read\nstring text = last >= start ? chars.GetText(Interval.Of(start, last)) : string.Empty;","handlingStrategy":"validation","validationCode":"int last = chars.Index - 1; // meaningful after EOF has been reached\nif (interval.a > last) text = string.Empty;\nelse text = chars.GetText(Interval.Of(interval.a, Math.Min(interval.b, last)));","typeGuard":null,"tryCatchPattern":"try { text = chars.GetText(interval); }\ncatch (ArgumentException ex) when (ex.Message == \"the interval extends past the end of the stream\") { /* clamp stop index and retry */ }","preventionTips":["Never use sentinel stop indexes such as int.MaxValue.","Extract text while at or before EOF with accurate token indexes.","Clamp requested ranges to the last valid character index."],"tags":["antlr","csharp","intervals","eof","char-stream"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}