{"record":{"id":"09fa63fa3db5b3b8","repo":"stanfordnlp/CoreNLP","slug":"invalid-nextbranchindex","errorCode":null,"errorMessage":"Invalid nextBranchIndex=","messagePattern":"Invalid nextBranchIndex=","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/SequenceMatcher.java","lineNumber":1182,"sourceCode":"      } else {\n        return null;\n      }\n    }\n\n    protected void setMatchedResult(int bid, int index, Object obj) {\n      if (index >= 0) {\n        Map<Integer,Object> matchedResults = getMatchedResults(bid, true);\n        Object oldObj = matchedResults.get(index);\n        if (oldObj != null) {\n          logger.warning(\"Setting matchedResult=\" + obj + \": index \" + index + \" already exists: \" + oldObj);\n        }\n        matchedResults.put(index, obj);\n      }\n    }\n\n    protected int getBranchId(int bid, int nextBranchIndex, int nextTotal) {\n      if (nextBranchIndex <= 0 || nextBranchIndex > nextTotal) {\n        throw new IllegalArgumentException(\"Invalid nextBranchIndex=\" + nextBranchIndex + \", nextTotal=\" + nextTotal);\n      }\n      if (nextTotal == 1) {\n        return bid;\n      } else {\n        Pair<Integer,Integer> p = new Pair<>(bid, nextBranchIndex);\n        int i = bidIndex.indexOf(p);\n        if (i < 0) {\n          for (int j = 0; j < nextTotal; j++) {\n            bidIndex.add(new Pair<>(bid, j + 1));\n          }\n          i = bidIndex.indexOf(p);\n        }\n        return i;\n      }\n    }\n\n    protected Map<SequencePattern.State,Object> getMatchStateInfo(int bid, boolean add) {\n      BranchState bs = getBranchState(bid, add);","sourceCodeStart":1164,"sourceCodeEnd":1200,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/SequenceMatcher.java#L1164-L1200","documentation":"getBranchId() in SequenceMatcher's branch-tracking state validates that the next branch index is within [1, nextTotal]; otherwise it throws IllegalArgumentException 'Invalid nextBranchIndex=..., nextTotal=...'. This is an internal invariant protecting the branch index bookkeeping used for backtracking alternatives.","triggerScenarios":"Reached when the matcher's internal branch state is inconsistent — e.g. nextBranchIndex computed as 0 or exceeding nextTotal during match enumeration with branching patterns — typically via getBranchId calls from matching internals.","commonSituations":"Rare in normal use; arises from bugs in custom pattern expression implementations, corrupted matcher state after concurrent or reentrant use, or deserialized matchers with stale branch indexes.","solutions":["Report/fix the pattern expression that produces an out-of-range nextBranchIndex","Avoid reusing or sharing a SequenceMatcher across threads; create a fresh matcher per search","Reset matcher state (via region/matcher re-creation) before reusing it for a new search","Catch IllegalArgumentException as a signal of internal-state corruption and rebuild the matcher"],"exampleFix":"// before\n// matcher reused across threads -> corrupted branch state\nSequenceMatcher<T> m = sharedMatcher;\nm.find();\n// after\nSequenceMatcher<T> m = pattern.matcher(elements);\nm.find();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  matcher.find();\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"Invalid nextBranchIndex=\")) {\n    log.error(\"Corrupted matcher branch state; rebuilding matcher\", e);\n    matcher = pattern.matcher(elements);\n  }\n}","preventionTips":["Never share a SequenceMatcher across threads","Create a fresh matcher per search instead of resetting state","Avoid custom PatternExpr subclasses that miscompute branch totals"],"tags":["java","illegal-argument","internal-state","tokensregex"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}