Skip to content

Conversation

gtoison
Copy link
Contributor

@gtoison gtoison commented Jan 19, 2025

As reported in #3266 FindReturnRef crashes on:

        @Override
        public Builder setCharset(final Charset charset) {
            nullCharset = charset == null;
            return super.setCharset(charset);
        }

This seems to be triggered by the CFG optimization in:

if (i instanceof IFNULL || i instanceof IFNONNULL) {
IfInstruction ii = (IfInstruction) i;
InstructionHandle target = ii.getTarget();
InstructionHandle next1 = head.getNext(); // ICONST
if (next1 == null) {
break;
}
if (next1.getInstruction() instanceof ICONST) {
InstructionHandle next2 = next1.getNext(); // GOTO
if (next2 == null) {
break;
}
InstructionHandle next3 = next2.getNext(); // ICONST
if (next3 == null) {
break;
}
InstructionHandle next4 = next3.getNext();
if (next4 == null) {
break;
}
if (target.equals(next3) && next2.getInstruction() instanceof GOTO
&& next3.getInstruction() instanceof ICONST && next1.getTargeters().length == 0
&& next2.getTargeters().length == 0 && next3.getTargeters().length == 1
&& next4.getTargeters().length == 1) {
int c1 = ((ICONST) next1.getInstruction()).getValue().intValue();
GOTO g = (GOTO) next2.getInstruction();
int c2 = ((ICONST) next3.getInstruction()).getValue().intValue();
if (g.getTarget().equals(next4) && (c1 == 1 && c2 == 0 || c1 == 0 && c2 == 1)) {
boolean nullIsTrue = i instanceof IFNULL && c2 == 1 || i instanceof IFNONNULL && c2 == 0;
if (nullIsTrue) {
// System.out.println("Found NULL2Z instruction");
head.swapInstruction(new NULL2Z());
} else {
// System.out.println("Found NONNULL2Z instruction");
head.swapInstruction(new NONNULL2Z());
}
next3.removeAllTargeters();
next4.removeAllTargeters();
next1.swapInstruction(new NOP());
next2.swapInstruction(new NOP());
next3.swapInstruction(new NOP());

For instance the following bytecode:

     0  aload_0 [this]
     1  aload_1 [x]
     2  ifnonnull 9
     5  iconst_1
     6  goto 10
     9  iconst_0
    10  putfield ghIssues.Issue3266.initialized : boolean [2]
    13  return

Is transformed to add the synthetic (made up) NONNULL2Z instruction and the instruction targeters are removed.

See #3080

This should fix #3266

@gtoison gtoison added the bug label Jan 19, 2025
In case the CFG was optimized the instruction targeters are removed
@gtoison gtoison marked this pull request as ready for review January 24, 2025 19:24
@gtoison
Copy link
Contributor Author

gtoison commented Jan 24, 2025

Although the symptoms are similar, #3265 is a different issue, I'll see if I can come up with a separate fix for it

@hazendaz hazendaz merged commit 9b8228e into spotbugs:master Jan 26, 2025
11 of 15 checks passed
@hazendaz hazendaz added this to the SpotBugs 4.9.1 milestone Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exception analyzing org.apache.commons.io.input.BOMInputStream using detector edu.umd.cs.findbugs.detect.FindReturnRef
2 participants