From dfc90938ba36685ef58af0846ee4bdb214fa210f Mon Sep 17 00:00:00 2001 From: Chen Liang Date: Tue, 17 Sep 2024 21:08:47 +0000 Subject: [PATCH] 8340132: Remove internal CpException for reading malformed utf8 Reviewed-by: asotona --- .../classfile/impl/AbstractPoolEntry.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractPoolEntry.java b/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractPoolEntry.java index 7f14560fd18..7ef005eaf6a 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractPoolEntry.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractPoolEntry.java @@ -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); - } - } }