Duplicate and ambiguous switch cases

Adam Sotona adam.sotona at oracle.com
Tue Jan 30 16:10:13 UTC 2024


Thank you for reporting it.
This can be a nice enhancement of the StackCounter error reporting.
Please let me know if you have troubles to create<https://bugs.openjdk.org/secure/CreateIssue!default.jspa> a new JDK Enhancement issue on core-libs / java.lang.classfile subcomponent.

Thank you,
Adam

From: classfile-api-dev <classfile-api-dev-retn at openjdk.org> on behalf of david32768 at btinternet.com david32768 at btinternet.com <david32768 at btinternet.com>
Date: Monday, 29 January 2024 at 7:46
To: classfile-api-dev at openjdk.org <classfile-api-dev at openjdk.org>
Subject: Duplicate and ambiguous switch cases



I apologise for the misleading comment about the error message.



If the bad lookup is picked up by StackMapGenerate (V65) it also prints the code.

If the bad lookup is picked up by StackCounter (V50) it does not.



```

import java.io.IOException;

import java.lang.constant.ClassDesc;

import java.lang.constant.MethodTypeDesc;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.util.List;

import java.util.ArrayList;



import java.lang.classfile.*;

import java.lang.classfile.instruction.*;



public class CBBuildDup {



private static void build(CodeBuilder cob) {

Label L1 = cob.newLabel();

Label L2 = cob.newLabel();

SwitchCase sc1 = SwitchCase.of(0,L2); // 0 -> L2

SwitchCase sc2 = SwitchCase.of(0,L2); // 0 -> L2 Duplicate

// SwitchCase sc2 = SwitchCase.of(0,L1); // 0 -> L1 Ambiguous

List<SwitchCase> cases = new ArrayList<>();

cases.add(sc1);

cases.add(sc2);

cob.iload(0)

.lookupswitch(L1, cases)

// .tableswitch(L1, cases)

.labelBinding(L2)

.iconst_1()

.ireturn()

.labelBinding(L1)

.iconst_0()

.ireturn();

}

public static void main(String[] args) throws IOException {

byte[] bytes = ClassFile.of()

.build(ClassDesc.of("CBSwitchDup"),

clb -> clb.withFlags(ClassFile.ACC_PUBLIC| ClassFile.ACC_SUPER)

// .withVersion(50,0) // bad lookup (StackCounter)

.withVersion(65,0) // bad lookup + code dump (StackMapGenerator)

.withMethodBody("table", MethodTypeDesc.ofDescriptor("(I)I"),

ClassFile.ACC_PUBLIC | ClassFile.ACC_STATIC, CBBuildDup::build));

for (VerifyError err:ClassFile.of().verify(bytes)) {

System.err.println(err);

}

Path pathc = Paths.get("CBSwitchDup.class");

Files.write(pathc,bytes);

}

}

```

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20240130/951c8b88/attachment.htm>


More information about the classfile-api-dev mailing list