Build TABLESWITCH from sorted arrays in SWITCH#518
Merged
garydgregory merged 1 commit intoJul 11, 2026
Conversation
The TABLESWITCH branch filled the table from the caller's unsorted match/targets arrays instead of the sorted clones the LOOKUPSWITCH branch uses, so an unsorted match array produced a table with out-of-order case values and a low/high header that disagrees with the emitted offset count.
There was a problem hiding this comment.
Pull request overview
This PR fixes incorrect TABLESWITCH generation in SWITCH when the caller provides an unsorted match array whose sorted form is gap-free. The TABLESWITCH table is now built from the already-sorted clones (matchClone/targetsClone), aligning behavior with the LOOKUPSWITCH path and preventing invalid low/high metadata vs. offset table size.
Changes:
- Build the
TABLESWITCHmVec/tVectable usingmatchCloneandtargetsClone(sorted clones) instead of the original input arrays. - Add a regression test ensuring an unsorted-but-gap-free
matchproduces aTABLESWITCHwith sorted case keys and correctly paired targets.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/main/java/org/apache/bcel/generic/SWITCH.java | Fixes TABLESWITCH table construction to use sorted clones, preventing malformed serialized switches. |
| src/test/java/org/apache/bcel/generic/SWITCHTest.java | Adds coverage for the unsorted-input, gap-free case to prevent regressions in TABLESWITCH generation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
garydgregory
added a commit
that referenced
this pull request
Jul 11, 2026
Member
|
Merged, thank you @rootvector2 🚀 |
garydgregory
added a commit
that referenced
this pull request
Jul 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SWITCHclones the caller'smatch/targetsarrays and sorts only the clones, but the TABLESWITCH branch fills the table from the original unsortedmatch/targets, while the LOOKUPSWITCH branch usesmatchClone/targetsClone. When the match array is passed unsorted (the class javadoc documents unsorted input and promises to leave the caller's arrays unaltered) and its sorted form is gap-free, the fill loop runs over unsorted keys, so the generatedTABLESWITCHgets out-of-order case values padded with default entries.TABLESWITCH.dumpthen writeslow = match[0]andhigh = match[last]that disagree with the count of offset words emitted, and re-reading the dumped instruction consumes onlyhigh - low + 1offsets, reinterpreting the surplus words as the next instructions.Fill the table from
matchClone/targetsCloneto match the LOOKUPSWITCH branch. Found while auditing the switch-generation paths.mvn; that'smvnon the command line by itself.