TableSwitch checks

david32768@btinternet.com david32768@btinternet.com david32768 at btinternet.com
Sat Nov 23 15:11:02 UTC 2024


The following are bugs or suggested improvements to the checking of 
TableSwitch instructions:
 
cases can be empty (if all targets are the default label)
which results in low > high.
```
***** SWITCH24-25\CodeBuilder.java
2428:          }
2429:          return tableswitch(low, high, defaultTarget, cases);
***** SWITCH_MODS\CODEBUILDER.JAVA
2428:          }
2429:          if (cases.isEmpty()) {
2430:              low = high = 0;
2431:          }
2432:          return tableswitch(low, high, defaultTarget, cases);
*****
```
low = Integer.MIN_VALUE, high = Integer.MAX_VALUE is too large a range.
Integer.MAX_VALUE - Integer.MIN_VALUE + 1 == 0
```
***** SWITCH24-25\StackCounter.java
 278:                              keys = high - low + 1;
 279:                              if (keys < 0) {
 280:                                  throw error("too many keys 
in tableswitch");
***** SWITCH_MODS\STACKCOUNTER.JAVA
 278:                              keys = high - low + 1;
 279:                              if (keys <= 0) {
 280:                                  throw error("too many keys 
in tableswitch");
*****
```
do same checks on UnboundInstruction as BoundInstruction?
```
***** SWITCH24-25\AbstractInstruction.java
 935:              this.highValue = highValue;
 936:              this.defaultTarget = requireNonNull(defaultTarget);
***** SWITCH_MODS\ABSTRACTINSTRUCTION.JAVA
 935:              this.highValue = highValue;
 936:              if (highValue < lowValue || (long)highValue - 
lowValue > (65535 - 4) >> 2 {
 937:                  throw new IllegalArgumentException("Invalid 
tableswitch values low: " + low + " high: " + high);
 938:              }
 939:              this.defaultTarget = requireNonNull(defaultTarget);
*****
```
check cases in range [low,high] and no duplicates (which can occur via 
UnboundInstruction)
```
***** SWITCH24-25\DirectCodeBuilder.java
 641:          for (var c : cases) {
 642:              caseMap.put(c.caseValue(), c.target());
 643:          }
***** SWITCH_MODS\DIRECTCODEBUILDER.JAVA
 641:          for (var c : cases) {
 642:              int cv = c.caseValue();
 643:              if (cv < low || cv > high) {
 644:                  throw new 
IllegalArgumentException(String.format("Case value %d is not in 
[low=%d,high=%d]", cv, low,high));
 645:              }
 646:              var previous = caseMap.put(cv, c.target());
 647:              if (previous != null) {
 648:                  throw new 
IllegalArgumentException(String.format("Duplicate case value 
%d", cv);
 649:              }
 650:          }
*****
```
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20241123/73a9b896/attachment.htm>


More information about the classfile-api-dev mailing list