{"record":{"id":"47012439138e2713","repo":"oracle/graal","slug":"got-illegal-regionto-value-d-regionto-must-be","errorCode":null,"errorMessage":"got illegal regionTo value: %d. regionTo must be >= regionFrom (%d) and <= input length (%d)","messagePattern":"got illegal regionTo value: (.+?)\\. regionTo must be >= regionFrom \\((.+?)\\) and <= input length \\((.+?)\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"regex/src/com.oracle.truffle.regex/src/com/oracle/truffle/regex/RegexExecNode.java","lineNumber":89,"sourceCode":"        int length = InputOps.length(input, getEncoding());\n        long fromIndex = toLongNode.execute(args[1]);\n        if (fromIndex > Integer.MAX_VALUE) {\n            return RegexResult.getNoMatchInstance();\n        }\n        final int toIndex;\n        final int regionFrom;\n        final int regionTo;\n        if (args.length == 5) {\n            long toIndexLong = toLongNode.execute(args[2]);\n            long regionFromLong = toLongNode.execute(args[3]);\n            long regionToLong = toLongNode.execute(args[4]);\n            if (regionFromLong < 0 || regionFromLong > length) {\n                CompilerDirectives.transferToInterpreterAndInvalidate();\n                throw new IllegalArgumentException(String.format(\"got illegal regionFrom value: %d. regionFrom must be >= 0 and <= input length (%d)\", regionFromLong, length));\n            }\n            if (regionToLong < regionFromLong || regionToLong > length) {\n                CompilerDirectives.transferToInterpreterAndInvalidate();\n                throw new IllegalArgumentException(String.format(\"got illegal regionTo value: %d. regionTo must be >= regionFrom (%d) and <= input length (%d)\", regionToLong, regionFromLong, length));\n            }\n            if (fromIndex < regionFromLong || fromIndex > regionToLong) {\n                CompilerDirectives.transferToInterpreterAndInvalidate();\n                throw new IllegalArgumentException(\n                                String.format(\"got illegal fromIndex value: %d. fromIndex must be >= regionFrom (%d) and <= regionTo (%d)\", fromIndex, regionFromLong, regionToLong));\n            }\n            if (toIndexLong < fromIndex || toIndexLong > regionToLong) {\n                CompilerDirectives.transferToInterpreterAndInvalidate();\n                throw new IllegalArgumentException(String.format(\"got illegal toIndex value: %d. toIndex must be >= fromIndex (%d) and <= regionTo (%d)\", toIndexLong, fromIndex, regionToLong));\n            }\n            if (toIndexLong != regionToLong) {\n                CompilerDirectives.transferToInterpreterAndInvalidate();\n                throw new UnsupportedOperationException(String.format(\"got non-equal toIndex (%d) and regionTo (%d), support for this is not implemented yet\", toIndexLong, regionToLong));\n            }\n            toIndex = (int) toIndexLong;\n            regionFrom = (int) regionFromLong;\n            regionTo = (int) regionToLong;\n        } else {","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/regex/src/com.oracle.truffle.regex/src/com/oracle/truffle/regex/RegexExecNode.java#L71-L107","documentation":"In RegexExecNode's 5-argument form, regionTo must satisfy regionFrom <= regionTo <= inputLength. A regionTo smaller than regionFrom or beyond the end of the input throws IllegalArgumentException with this message.","triggerScenarios":"Calling the 5-arg regex exec where the 5th argument is less than the 4th (regionTo < regionFrom) or greater than the input length, e.g. regionFrom=10, regionTo=5, or regionTo=input.length()+2.","commonSituations":"Guest-language adapters translating APIs like Java Matcher.region(from, to) with swapped or off-by-one arguments; code that copies region bounds from a previous match on a longer string; unterminated searches where regionTo defaults to -1.","solutions":["Normalize arguments so regionTo = Math.max(regionFrom, Math.min(regionTo, inputLength)).","Verify argument order at the call site (from before to) when bridging from another regex API.","Recompute region bounds from the current input string rather than reusing stale ones."],"exampleFix":"// before\nexec(input, 0, len, from, to); // to < from when caller swaps bounds\n\n// after\nif (to < from) { int t = from; from = to; to = t; }\nto = Math.min(to, input.length());\nexec(input, 0, to, from, to);","handlingStrategy":"validation","validationCode":"static boolean validRegionTo(long regionFrom, long regionTo, int inputLength) {\n    return regionTo >= regionFrom && regionTo <= inputLength;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always order region bounds (from <= to) after any user-supplied values.","Never carry regionTo from a previous longer input into the next call.","Unit-test the adapter with empty strings and full-length regions."],"tags":["regex","truffle","graalvm","argument-validation","region"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}