8340132: Remove internal CpException for reading malformed utf8

Reviewed-by: asotona
This commit is contained in:
Chen Liang 2024-09-17 21:08:47 +00:00
parent f0ae90f30c
commit dfc90938ba

View file

@ -24,6 +24,7 @@
*/
package jdk.internal.classfile.impl;
import java.lang.classfile.constantpool.ConstantPoolException;
import java.lang.constant.*;
import java.lang.invoke.TypeDescriptor;
import java.nio.charset.StandardCharsets;
@ -258,11 +259,11 @@ public abstract sealed class AbstractPoolEntry {
// 110x xxxx 10xx xxxx
px += 2;
if (px > utfend) {
throw new CpException("malformed input: partial character at end");
throw malformedInput(utfend);
}
int char2 = rawBytes[px - 1];
if ((char2 & 0xC0) != 0x80) {
throw new CpException("malformed input around byte " + px);
throw malformedInput(px);
}
char v = (char) (((c & 0x1F) << 6) | (char2 & 0x3F));
chararr[chararr_count++] = v;
@ -273,12 +274,12 @@ public abstract sealed class AbstractPoolEntry {
// 1110 xxxx 10xx xxxx 10xx xxxx
px += 3;
if (px > utfend) {
throw new CpException("malformed input: partial character at end");
throw malformedInput(utfend);
}
int char2 = rawBytes[px - 2];
int char3 = rawBytes[px - 1];
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) {
throw new CpException("malformed input around byte " + (px - 1));
throw malformedInput(px - 1);
}
char v = (char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | (char3 & 0x3F));
chararr[chararr_count++] = v;
@ -287,7 +288,7 @@ public abstract sealed class AbstractPoolEntry {
}
default:
// 10xx xxxx, 1111 xxxx
throw new CpException("malformed input around byte " + px);
throw malformedInput(px);
}
}
this.hash = hashString(hash);
@ -295,7 +296,10 @@ public abstract sealed class AbstractPoolEntry {
this.chars = chararr;
state = State.CHAR;
}
}
private ConstantPoolException malformedInput(int px) {
return new ConstantPoolException("#%d: malformed modified UTF8 around byte %d".formatted(index(), px));
}
@Override
@ -1134,12 +1138,4 @@ public abstract sealed class AbstractPoolEntry {
return false;
}
}
static class CpException extends RuntimeException {
static final long serialVersionUID = 32L;
CpException(String s) {
super(s);
}
}
}