Skip to content

8361635: Missing List length validation in the Class-File API #26252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: pr/26201
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ default ClassDesc classSymbol() {
* @param annotationClass the constant pool entry holding the descriptor string
* of the annotation interface
* @param elements the element-value pairs of the annotation
* @throws IllegalArgumentException if the number of pairs exceeds {@code
* 65535}
*/
static Annotation of(Utf8Entry annotationClass,
List<AnnotationElement> elements) {
Expand All @@ -106,6 +108,8 @@ static Annotation of(Utf8Entry annotationClass,
* @param annotationClass the constant pool entry holding the descriptor string
* of the annotation interface
* @param elements the element-value pairs of the annotation
* @throws IllegalArgumentException if the number of pairs exceeds {@code
* 65535}
*/
static Annotation of(Utf8Entry annotationClass,
AnnotationElement... elements) {
Expand All @@ -116,6 +120,8 @@ static Annotation of(Utf8Entry annotationClass,
* {@return an annotation}
* @param annotationClass the descriptor of the annotation interface
* @param elements the element-value pairs of the annotation
* @throws IllegalArgumentException if the number of pairs exceeds {@code
* 65535}
*/
static Annotation of(ClassDesc annotationClass,
List<AnnotationElement> elements) {
Expand All @@ -126,6 +132,8 @@ static Annotation of(ClassDesc annotationClass,
* {@return an annotation}
* @param annotationClass the descriptor of the annotation interface
* @param elements the element-value pairs of the annotation
* @throws IllegalArgumentException if the number of pairs exceeds {@code
* 65535}
*/
static Annotation of(ClassDesc annotationClass,
AnnotationElement... elements) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ static AnnotationElement ofAnnotation(String name,
* @param name the name of the key
* @param values the associated values
* @see AnnotationValue#ofArray(AnnotationValue...) AnnotationValue::ofArray
* @throws IllegalArgumentException if the number of associated values
* exceeds {@code 65535}
*/
static AnnotationElement ofArray(String name,
AnnotationValue... values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,8 @@ static OfAnnotation ofAnnotation(Annotation value) {
* on array values derived from Java source code.
*
* @param values the array elements
* @throws IllegalArgumentException if the length of array exceeds {@code
* 65535}
*/
static OfArray ofArray(List<AnnotationValue> values) {
return new AnnotationImpl.OfArrayImpl(values);
Expand All @@ -686,6 +688,8 @@ static OfArray ofArray(List<AnnotationValue> values) {
* on array values derived from Java source code.
*
* @param values the array elements
* @throws IllegalArgumentException if the length of array exceeds {@code
* 65535}
*/
static OfArray ofArray(AnnotationValue... values) {
return ofArray(List.of(values));
Expand All @@ -699,7 +703,8 @@ static OfArray ofArray(AnnotationValue... values) {
* @param value the annotation value
* @throws IllegalArgumentException when the {@code value} parameter is not
* a primitive, a wrapper of primitive, a String, a ClassDesc,
* an enum constant, or an array of one of these.
* an enum constant, or an array of one of these; or an array has
* length over {@code 65535}
*/
static AnnotationValue of(Object value) {
if (value instanceof String s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ default ClassBuilder withSuperclass(ClassDesc desc) {
*
* @param interfaces the interfaces
* @return this builder
* @throws IllegalArgumentException if the number of interfaces exceeds {@code 65535}
* @see Interfaces
*/
default ClassBuilder withInterfaces(List<ClassEntry> interfaces) {
Expand All @@ -142,6 +143,7 @@ default ClassBuilder withInterfaces(List<ClassEntry> interfaces) {
*
* @param interfaces the interfaces
* @return this builder
* @throws IllegalArgumentException if the number of interfaces exceeds {@code 65535}
* @see Interfaces
*/
default ClassBuilder withInterfaces(ClassEntry... interfaces) {
Expand All @@ -153,7 +155,8 @@ default ClassBuilder withInterfaces(ClassEntry... interfaces) {
*
* @param interfaces the interfaces
* @return this builder
* @throws IllegalArgumentException if any element of {@code interfaces} is primitive
* @throws IllegalArgumentException if any of {@code interfaces} is primitive,
* or if the number of interfaces exceeds {@code 65535}
* @see Interfaces
*/
default ClassBuilder withInterfaceSymbols(List<ClassDesc> interfaces) {
Expand All @@ -165,7 +168,8 @@ default ClassBuilder withInterfaceSymbols(List<ClassDesc> interfaces) {
*
* @param interfaces the interfaces
* @return this builder
* @throws IllegalArgumentException if any element of {@code interfaces} is primitive
* @throws IllegalArgumentException if any of {@code interfaces} is primitive,
* or if the number of interfaces exceeds {@code 65535}
* @see Interfaces
*/
default ClassBuilder withInterfaceSymbols(ClassDesc... interfaces) {
Expand Down
10 changes: 8 additions & 2 deletions src/java.base/share/classes/java/lang/classfile/Interfaces.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public sealed interface Interfaces
/**
* {@return an {@linkplain Interfaces} element}
* @param interfaces the interfaces
* @throws IllegalArgumentException if the number of interfaces
* exceeds {@code 65535}
*/
static Interfaces of(List<ClassEntry> interfaces) {
return new InterfacesImpl(interfaces);
Expand All @@ -62,6 +64,8 @@ static Interfaces of(List<ClassEntry> interfaces) {
/**
* {@return an {@linkplain Interfaces} element}
* @param interfaces the interfaces
* @throws IllegalArgumentException if the number of interfaces
* exceeds {@code 65535}
*/
static Interfaces of(ClassEntry... interfaces) {
return of(List.of(interfaces));
Expand All @@ -70,7 +74,8 @@ static Interfaces of(ClassEntry... interfaces) {
/**
* {@return an {@linkplain Interfaces} element}
* @param interfaces the interfaces
* @throws IllegalArgumentException if any of {@code interfaces} is primitive
* @throws IllegalArgumentException if any of {@code interfaces} is primitive,
* or if the number of interfaces exceeds {@code 65535}
*/
static Interfaces ofSymbols(List<ClassDesc> interfaces) {
return of(Util.entryList(interfaces));
Expand All @@ -79,7 +84,8 @@ static Interfaces ofSymbols(List<ClassDesc> interfaces) {
/**
* {@return an {@linkplain Interfaces} element}
* @param interfaces the interfaces
* @throws IllegalArgumentException if any of {@code interfaces} is primitive
* @throws IllegalArgumentException if any of {@code interfaces} is primitive,
* or if the number of interfaces exceeds {@code 65535}
*/
static Interfaces ofSymbols(ClassDesc... interfaces) {
return ofSymbols(Arrays.asList(interfaces));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ public int sizeIfFixed() {
* @param targetInfo which type in a declaration or expression is annotated
* @param targetPath which part of the type is annotated
* @param annotation the annotation
* @throws IllegalArgumentException if the size of {@code targetPath}
* exceeds {@code 255}
*/
static TypeAnnotation of(TargetInfo targetInfo, List<TypePathComponent> targetPath,
Annotation annotation) {
Expand Down Expand Up @@ -486,6 +488,8 @@ static ThrowsTarget ofThrows(int throwsTargetIndex) {
* including a variable declared as a resource in a try-with-resources statement}
* @param targetType {@link TargetType#LOCAL_VARIABLE} or {@link TargetType#RESOURCE_VARIABLE}
* @param table the list of local variable targets
* @throws IllegalArgumentException if the size of the list of targets
* exceeds {@code 65535}
*/
static LocalVarTarget ofVariable(TargetType targetType, List<LocalVarTargetInfo> table) {
return new TargetInfoImpl.LocalVarTargetImpl(targetType, table);
Expand All @@ -494,6 +498,8 @@ static LocalVarTarget ofVariable(TargetType targetType, List<LocalVarTargetInfo>
/**
* {@return a target for annotations on the type in a local variable declaration}
* @param table the list of local variable targets
* @throws IllegalArgumentException if the size of the list of targets
* exceeds {@code 65535}
*/
static LocalVarTarget ofLocalVariable(List<LocalVarTargetInfo> table) {
return ofVariable(TargetType.LOCAL_VARIABLE, table);
Expand All @@ -503,6 +509,8 @@ static LocalVarTarget ofLocalVariable(List<LocalVarTargetInfo> table) {
* {@return a target for annotations on the type in a local variable declared
* as a resource in a try-with-resources statement}
* @param table the list of local variable targets
* @throws IllegalArgumentException if the size of the list of targets
* exceeds {@code 65535}
*/
static LocalVarTarget ofResourceVariable(List<LocalVarTargetInfo> table) {
return ofVariable(TargetType.RESOURCE_VARIABLE, table);
Expand Down Expand Up @@ -802,7 +810,6 @@ sealed interface LocalVarTargetInfo
*/
Label startLabel();


/**
* The given local variable has a value at indices into the code array in the interval
* [start_pc, start_pc + length), that is, between start_pc inclusive and start_pc + length exclusive.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public sealed interface CharacterRangeTableAttribute
* {@link CodeBuilder#characterRange CodeBuilder::characterRange} instead.
*
* @param ranges the descriptions of the character ranges
* @throws IllegalArgumentException if the number of ranges exceeds {@code 65535}
*/
static CharacterRangeTableAttribute of(List<CharacterRangeInfo> ranges) {
return new UnboundAttribute.UnboundCharacterRangeTableAttribute(ranges);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public sealed interface ExceptionsAttribute
/**
* {@return an {@code Exceptions} attribute}
* @param exceptions the exceptions that may be thrown from this method
* @throws IllegalArgumentException if the number of exceptions exceeds {@code 65535}
*/
static ExceptionsAttribute of(List<ClassEntry> exceptions) {
return new UnboundAttribute.UnboundExceptionsAttribute(exceptions);
Expand All @@ -85,6 +86,7 @@ static ExceptionsAttribute of(List<ClassEntry> exceptions) {
/**
* {@return an {@code Exceptions} attribute}
* @param exceptions the exceptions that may be thrown from this method
* @throws IllegalArgumentException if the number of exceptions exceeds {@code 65535}
*/
static ExceptionsAttribute of(ClassEntry... exceptions) {
return of(List.of(exceptions));
Expand All @@ -93,6 +95,7 @@ static ExceptionsAttribute of(ClassEntry... exceptions) {
/**
* {@return an {@code Exceptions} attribute}
* @param exceptions the exceptions that may be thrown from this method
* @throws IllegalArgumentException if the number of exceptions exceeds {@code 65535}
*/
static ExceptionsAttribute ofSymbols(List<ClassDesc> exceptions) {
return of(Util.entryList(exceptions));
Expand All @@ -101,6 +104,7 @@ static ExceptionsAttribute ofSymbols(List<ClassDesc> exceptions) {
/**
* {@return an {@code Exceptions} attribute}
* @param exceptions the exceptions that may be thrown from this method
* @throws IllegalArgumentException if the number of exceptions exceeds {@code 65535}
*/
static ExceptionsAttribute ofSymbols(ClassDesc... exceptions) {
return ofSymbols(Arrays.asList(exceptions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public sealed interface InnerClassesAttribute
/**
* {@return an {@code InnerClasses} attribute}
* @param innerClasses descriptions of the nested classes
* @throws IllegalArgumentException if the number of descriptions exceeds {@code 65535}
*/
static InnerClassesAttribute of(List<InnerClassInfo> innerClasses) {
return new UnboundAttribute.UnboundInnerClassesAttribute(innerClasses);
Expand All @@ -73,6 +74,7 @@ static InnerClassesAttribute of(List<InnerClassInfo> innerClasses) {
/**
* {@return an {@code InnerClasses} attribute}
* @param innerClasses descriptions of the nested classes
* @throws IllegalArgumentException if the number of descriptions exceeds {@code 65535}
*/
static InnerClassesAttribute of(InnerClassInfo... innerClasses) {
return new UnboundAttribute.UnboundInnerClassesAttribute(List.of(innerClasses));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public sealed interface LineNumberTableAttribute
* order instead.
*
* @param lines the line number descriptions
* @throws IllegalArgumentException if the number of descriptions exceeds {@code 65535}
*/
static LineNumberTableAttribute of(List<LineNumberInfo> lines) {
return new UnboundAttribute.UnboundLineNumberTableAttribute(lines);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public sealed interface LocalVariableTableAttribute
* {@link CodeBuilder#localVariable CodeBuilder::localVariable} instead.
*
* @param locals the local variable descriptions
* @throws IllegalArgumentException if the number of descriptions exceeds {@code 65535}
*/
static LocalVariableTableAttribute of(List<LocalVariableInfo> locals) {
return new UnboundAttribute.UnboundLocalVariableTableAttribute(locals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public sealed interface LocalVariableTypeTableAttribute
/**
* {@return a {@code LocalVariableTypeTable} attribute}
* @param locals the local variable descriptions
* @throws IllegalArgumentException if the number of descriptions exceeds {@code 65535}
*/
static LocalVariableTypeTableAttribute of(List<LocalVariableTypeInfo> locals) {
return new UnboundAttribute.UnboundLocalVariableTypeTableAttribute(locals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public sealed interface MethodParametersAttribute
/**
* {@return a {@code MethodParameters} attribute}
* @param parameters the method parameter descriptions
* @throws IllegalArgumentException if the number of parameters exceeds {@code 255}
*/
static MethodParametersAttribute of(List<MethodParameterInfo> parameters) {
return new UnboundAttribute.UnboundMethodParametersAttribute(parameters);
Expand All @@ -76,6 +77,7 @@ static MethodParametersAttribute of(List<MethodParameterInfo> parameters) {
/**
* {@return a {@code MethodParameters} attribute}
* @param parameters the method parameter descriptions
* @throws IllegalArgumentException if the number of parameters exceeds {@code 255}
*/
static MethodParametersAttribute of(MethodParameterInfo... parameters) {
return of(List.of(parameters));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ default boolean has(AccessFlag flag) {
* @param uses the consumed services
* @param provides the provided services
* @throws IllegalArgumentException if {@code moduleFlags} is not in the
* range {@code [0, 65535]}
* range {@code [0, 65535]}, or any of the collections have more than
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I take it back. It is OK.

* {@code 65535} elements
*/
static ModuleAttribute of(ModuleEntry moduleName, int moduleFlags,
Utf8Entry moduleVersion,
Expand All @@ -188,6 +189,9 @@ static ModuleAttribute of(ModuleEntry moduleName, int moduleFlags,
*
* @param moduleName the module name
* @param attrHandler a handler that receives a {@link ModuleAttributeBuilder}
* @throws IllegalArgumentException if the information from the handler exceeds
* the {@code class} file format limit, such as over {@code 65535}
* requirements
*/
static ModuleAttribute of(ModuleDesc moduleName,
Consumer<ModuleAttributeBuilder> attrHandler) {
Expand Down Expand Up @@ -296,6 +300,7 @@ default ModuleAttributeBuilder requires(ModuleDesc module, Collection<AccessFlag
* @param exportsFlagsMask the export flags
* @param exportsToModules the modules to export to, or empty for an unqualified export
* @return this builder
* @throws IllegalArgumentException if the number of modules exceeds {@code 65535}
*/
ModuleAttributeBuilder exports(PackageDesc pkge, int exportsFlagsMask, ModuleDesc... exportsToModules);

Expand All @@ -308,6 +313,7 @@ default ModuleAttributeBuilder requires(ModuleDesc module, Collection<AccessFlag
* @return this builder
* @throws IllegalArgumentException if any flag cannot be applied to the
* {@link AccessFlag.Location#MODULE_EXPORTS} location
* @throws IllegalArgumentException if the number of modules exceeds {@code 65535}
*/
default ModuleAttributeBuilder exports(PackageDesc pkge, Collection<AccessFlag> exportsFlags, ModuleDesc... exportsToModules) {
return exports(pkge, Util.flagsToBits(AccessFlag.Location.MODULE_EXPORTS, exportsFlags), exportsToModules);
Expand All @@ -333,6 +339,7 @@ default ModuleAttributeBuilder exports(PackageDesc pkge, Collection<AccessFlag>
* @param opensFlagsMask the open package flags
* @param opensToModules the modules to open to, or empty for an unqualified open
* @return this builder
* @throws IllegalArgumentException if the number of modules exceeds {@code 65535}
*/
ModuleAttributeBuilder opens(PackageDesc pkge, int opensFlagsMask, ModuleDesc... opensToModules);

Expand All @@ -349,7 +356,8 @@ default ModuleAttributeBuilder exports(PackageDesc pkge, Collection<AccessFlag>
* @param opensToModules the modules to open to, or empty for an unqualified open
* @return this builder
* @throws IllegalArgumentException if any flag cannot be applied to the
* {@link AccessFlag.Location#MODULE_OPENS} location
* {@link AccessFlag.Location#MODULE_OPENS} location, or if the
* number of modules exceeds {@code 65535}
*/
default ModuleAttributeBuilder opens(PackageDesc pkge, Collection<AccessFlag> opensFlags, ModuleDesc... opensToModules) {
return opens(pkge, Util.flagsToBits(AccessFlag.Location.MODULE_OPENS, opensFlags), opensToModules);
Expand Down Expand Up @@ -391,7 +399,9 @@ default ModuleAttributeBuilder opens(PackageDesc pkge, Collection<AccessFlag> op
* @param service the service class provided
* @param implClasses the implementation classes
* @return this builder
* @throws IllegalArgumentException if {@code service} or any of the {@code implClasses} represents a primitive type
* @throws IllegalArgumentException if {@code service} or any of the
* {@code implClasses} represents a primitive type, or the
* number of implementations exceeds {@code 65535}
*/
ModuleAttributeBuilder provides(ClassDesc service, ClassDesc... implClasses);

Expand Down
Loading