Match wide local variable instruction length to dumped bytes#525
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bytecode round-tripping inconsistency for wide-prefixed local-variable load/store instructions by ensuring the parsed instruction length matches what dump() will actually emit. This prevents InstructionList.setPositions() from accumulating an incorrect length and shifting subsequent instruction offsets when rebuilding methods.
Changes:
- Update
LocalVariableInstruction.initFromFile()to set length based on the samewide()condition used bydump(), instead of always forcing length 4 forwide-prefixed parses. - Add JUnit tests covering
wide iload/istorecases where the parsed encoding is 4 bytes but the dumped encoding can be 2 bytes when the index fits in a byte. - Ensure large-index
wideinstructions remain 4 bytes and round-trip consistently.
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/LocalVariableInstruction.java | Aligns parsed length with emitted bytes by deriving length from wide() after reading the index. |
| src/test/java/org/apache/bcel/generic/LocalVariableInstructionTest.java | Adds regression tests asserting getLength() matches dump() output for wide/non-wide edge cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
garydgregory
added a commit
that referenced
this pull request
Jul 18, 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.
LocalVariableInstruction.initFromFilehardcodes length 4 for anywide-prefixed load or store, butdump()writes thewideprefix only when the index is aboveConst.MAX_BYTE. Awide iload 5is legal bytecode whose index fits in a byte, so it parses withgetLength()4 whiledump()emits 2 bytes. I hit this auditingwidehandling againstRETandIINC, which persist the parsed flag and keep the two in sync.InstructionList.setPositionsaccumulatesgetLength(), so rebuilding a parsed method that carries one of these instructions shifts every following instruction 2 bytes and corrupts the branch offsets that span it. Deriving the parsed length from the samewide()testdump()already uses keeps length and emitted bytes in agreement, leaving the compact and non-wide paths untouched.mvn; that'smvnon the command line by itself.