<div dir="ltr">Hi Adam,<div>This patch is simple; since this can prevent a lot of bugs around malicious CP references in crafted classfiles, should we consider this enhancement for JDK 23, or should we only have this as internal APIs in ClassReaderImpl?</div><div><br></div><div>- Chen</div></div><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, May 20, 2024 at 9:08 AM - <<a href="mailto:liangchenblue@gmail.com" target="_blank">liangchenblue@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi ClassFile API list,<div>I call for addition of a type-checked entryByIndex in ConstantPool, with a signature:</div><div><T extends PoolEntry> T entryByIndex(int index, Class<T> cls)<br></div><div>and a type-checked readEntryOrNull in ClassReader, with a signature:</div><div><T extends PoolEntry> T readEntryOrNull(int offset, Class<T> cls)</div><div>Which will throw ConstantPoolException if the entry is of a mismatched type, much like the type-checked readClassEntry in ClassBuilder.</div><div><br></div><div>A search for existing generic ConstantPool::entryByIndex and ClassReader::readEntryOrNull in JDK reveals that most of their usages within the jdk.internal.classfile.impl and its subpackages involve a direct cast right after retrieving the result. These casts are susceptible to malformed classfiles putting entries of wrong type, such as a Utf8 at the cursor of superclass entry, throwing ClassCastException, which is out of spec with the Classfile API.</div><div><br></div><div>I recommend adding these 2 methods for user convenience, and migrating all existing entryByIndex/readEntryOrNull with casts to these 2 new methods, to enhance the robustness of the ClassFile API. (On a side note, we can promote ClassReader::utf8EntryByIndex to ConstantPool too)</div><div><br></div><div>Please feel free to comment or critique this proposal.</div><div><br></div><div>Chen Liang</div></div>
</blockquote></div>