Skip to content

Commit 431f467

Browse files
committed
8361635: Missing List length validation in the Class-File API
Reviewed-by: asotona
1 parent 8d23661 commit 431f467

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+866
-111
lines changed

src/java.base/share/classes/java/lang/classfile/Annotation.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ default ClassDesc classSymbol() {
9595
* @param annotationClass the constant pool entry holding the descriptor string
9696
* of the annotation interface
9797
* @param elements the element-value pairs of the annotation
98+
* @throws IllegalArgumentException if the number of pairs exceeds the limit
99+
* of {@link java.lang.classfile##u2 u2}
98100
*/
99101
static Annotation of(Utf8Entry annotationClass,
100102
List<AnnotationElement> elements) {
@@ -106,6 +108,8 @@ static Annotation of(Utf8Entry annotationClass,
106108
* @param annotationClass the constant pool entry holding the descriptor string
107109
* of the annotation interface
108110
* @param elements the element-value pairs of the annotation
111+
* @throws IllegalArgumentException if the number of pairs exceeds the limit
112+
* of {@link java.lang.classfile##u2 u2}
109113
*/
110114
static Annotation of(Utf8Entry annotationClass,
111115
AnnotationElement... elements) {
@@ -116,6 +120,8 @@ static Annotation of(Utf8Entry annotationClass,
116120
* {@return an annotation}
117121
* @param annotationClass the descriptor of the annotation interface
118122
* @param elements the element-value pairs of the annotation
123+
* @throws IllegalArgumentException if the number of pairs exceeds the limit
124+
* of {@link java.lang.classfile##u2 u2}
119125
*/
120126
static Annotation of(ClassDesc annotationClass,
121127
List<AnnotationElement> elements) {
@@ -126,6 +132,8 @@ static Annotation of(ClassDesc annotationClass,
126132
* {@return an annotation}
127133
* @param annotationClass the descriptor of the annotation interface
128134
* @param elements the element-value pairs of the annotation
135+
* @throws IllegalArgumentException if the number of pairs exceeds the limit
136+
* of {@link java.lang.classfile##u2 u2}
129137
*/
130138
static Annotation of(ClassDesc annotationClass,
131139
AnnotationElement... elements) {

src/java.base/share/classes/java/lang/classfile/AnnotationElement.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ static AnnotationElement ofAnnotation(String name,
209209
* @param name the name of the key
210210
* @param values the associated values
211211
* @see AnnotationValue#ofArray(AnnotationValue...) AnnotationValue::ofArray
212+
* @throws IllegalArgumentException if the number of associated values
213+
* exceeds the limit of {@link java.lang.classfile##u2 u2}
212214
*/
213215
static AnnotationElement ofArray(String name,
214216
AnnotationValue... values) {

src/java.base/share/classes/java/lang/classfile/AnnotationValue.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,8 @@ static OfAnnotation ofAnnotation(Annotation value) {
673673
* on array values derived from Java source code.
674674
*
675675
* @param values the array elements
676+
* @throws IllegalArgumentException if the length of array exceeds the limit
677+
* of {@link java.lang.classfile##u2 u2}
676678
*/
677679
static OfArray ofArray(List<AnnotationValue> values) {
678680
return new AnnotationImpl.OfArrayImpl(values);
@@ -686,6 +688,8 @@ static OfArray ofArray(List<AnnotationValue> values) {
686688
* on array values derived from Java source code.
687689
*
688690
* @param values the array elements
691+
* @throws IllegalArgumentException if the length of array exceeds the limit
692+
* of {@link java.lang.classfile##u2 u2}
689693
*/
690694
static OfArray ofArray(AnnotationValue... values) {
691695
return ofArray(List.of(values));
@@ -699,7 +703,8 @@ static OfArray ofArray(AnnotationValue... values) {
699703
* @param value the annotation value
700704
* @throws IllegalArgumentException when the {@code value} parameter is not
701705
* a primitive, a wrapper of primitive, a String, a ClassDesc,
702-
* an enum constant, or an array of one of these.
706+
* an enum constant, or an array of one of these; or any array has
707+
* length over the limit of {@link java.lang.classfile##u2 u2}
703708
*/
704709
static AnnotationValue of(Object value) {
705710
if (value instanceof String s) {

src/java.base/share/classes/java/lang/classfile/ClassBuilder.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ default ClassBuilder withSuperclass(ClassDesc desc) {
131131
*
132132
* @param interfaces the interfaces
133133
* @return this builder
134+
* @throws IllegalArgumentException if the number of interfaces exceeds the
135+
* limit of {@link java.lang.classfile##u2 u2}
134136
* @see Interfaces
135137
*/
136138
default ClassBuilder withInterfaces(List<ClassEntry> interfaces) {
@@ -142,6 +144,8 @@ default ClassBuilder withInterfaces(List<ClassEntry> interfaces) {
142144
*
143145
* @param interfaces the interfaces
144146
* @return this builder
147+
* @throws IllegalArgumentException if the number of interfaces exceeds the
148+
* limit of {@link java.lang.classfile##u2 u2}
145149
* @see Interfaces
146150
*/
147151
default ClassBuilder withInterfaces(ClassEntry... interfaces) {
@@ -153,7 +157,9 @@ default ClassBuilder withInterfaces(ClassEntry... interfaces) {
153157
*
154158
* @param interfaces the interfaces
155159
* @return this builder
156-
* @throws IllegalArgumentException if any element of {@code interfaces} is primitive
160+
* @throws IllegalArgumentException if any of {@code interfaces} is primitive,
161+
* or if the number of interfaces exceeds the limit of {@link
162+
* java.lang.classfile##u2 u2}
157163
* @see Interfaces
158164
*/
159165
default ClassBuilder withInterfaceSymbols(List<ClassDesc> interfaces) {
@@ -165,7 +171,9 @@ default ClassBuilder withInterfaceSymbols(List<ClassDesc> interfaces) {
165171
*
166172
* @param interfaces the interfaces
167173
* @return this builder
168-
* @throws IllegalArgumentException if any element of {@code interfaces} is primitive
174+
* @throws IllegalArgumentException if any of {@code interfaces} is primitive,
175+
* or if the number of interfaces exceeds the limit of {@link
176+
* java.lang.classfile##u2 u2}
169177
* @see Interfaces
170178
*/
171179
default ClassBuilder withInterfaceSymbols(ClassDesc... interfaces) {

src/java.base/share/classes/java/lang/classfile/CodeBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,8 +2402,8 @@ default CodeBuilder ifne(Label target) {
24022402
* variable by a constant.
24032403
* <p>
24042404
* This may also generate {@link Opcode#IINC_W wide iinc} instructions if
2405-
* {@code slot} exceeds {@code 255} or {@code val} exceeds the range of
2406-
* {@link TypeKind#BYTE byte}.
2405+
* {@code slot} exceeds the limit of {@link java.lang.classfile##u1 u1} or
2406+
* {@code val} exceeds the range of {@link TypeKind#BYTE byte}.
24072407
*
24082408
* @param slot the local variable slot
24092409
* @param val the increment value

src/java.base/share/classes/java/lang/classfile/Interfaces.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public sealed interface Interfaces
5454
/**
5555
* {@return an {@linkplain Interfaces} element}
5656
* @param interfaces the interfaces
57+
* @throws IllegalArgumentException if the number of interfaces
58+
* exceeds the limit of {@link java.lang.classfile##u2 u2}
5759
*/
5860
static Interfaces of(List<ClassEntry> interfaces) {
5961
return new InterfacesImpl(interfaces);
@@ -62,6 +64,8 @@ static Interfaces of(List<ClassEntry> interfaces) {
6264
/**
6365
* {@return an {@linkplain Interfaces} element}
6466
* @param interfaces the interfaces
67+
* @throws IllegalArgumentException if the number of interfaces
68+
* exceeds the limit of {@link java.lang.classfile##u2 u2}
6569
*/
6670
static Interfaces of(ClassEntry... interfaces) {
6771
return of(List.of(interfaces));
@@ -70,7 +74,9 @@ static Interfaces of(ClassEntry... interfaces) {
7074
/**
7175
* {@return an {@linkplain Interfaces} element}
7276
* @param interfaces the interfaces
73-
* @throws IllegalArgumentException if any of {@code interfaces} is primitive
77+
* @throws IllegalArgumentException if any of {@code interfaces} is primitive,
78+
* or if the number of interfaces exceeds the limit of {@link
79+
* java.lang.classfile##u2 u2}
7480
*/
7581
static Interfaces ofSymbols(List<ClassDesc> interfaces) {
7682
return of(Util.entryList(interfaces));
@@ -79,7 +85,9 @@ static Interfaces ofSymbols(List<ClassDesc> interfaces) {
7985
/**
8086
* {@return an {@linkplain Interfaces} element}
8187
* @param interfaces the interfaces
82-
* @throws IllegalArgumentException if any of {@code interfaces} is primitive
88+
* @throws IllegalArgumentException if any of {@code interfaces} is primitive,
89+
* or if the number of interfaces exceeds the limit of {@link
90+
* java.lang.classfile##u2 u2}
8391
*/
8492
static Interfaces ofSymbols(ClassDesc... interfaces) {
8593
return ofSymbols(Arrays.asList(interfaces));

src/java.base/share/classes/java/lang/classfile/TypeAnnotation.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ public int sizeIfFixed() {
182182
* @param targetInfo which type in a declaration or expression is annotated
183183
* @param targetPath which part of the type is annotated
184184
* @param annotation the annotation
185+
* @throws IllegalArgumentException if the size of {@code targetPath}
186+
* exceeds the limit of {@link java.lang.classfile##u1 u1}
185187
*/
186188
static TypeAnnotation of(TargetInfo targetInfo, List<TypePathComponent> targetPath,
187189
Annotation annotation) {
@@ -486,6 +488,8 @@ static ThrowsTarget ofThrows(int throwsTargetIndex) {
486488
* including a variable declared as a resource in a try-with-resources statement}
487489
* @param targetType {@link TargetType#LOCAL_VARIABLE} or {@link TargetType#RESOURCE_VARIABLE}
488490
* @param table the list of local variable targets
491+
* @throws IllegalArgumentException if the size of the list of targets
492+
* exceeds the limit of {@link java.lang.classfile##u2 u2}
489493
*/
490494
static LocalVarTarget ofVariable(TargetType targetType, List<LocalVarTargetInfo> table) {
491495
return new TargetInfoImpl.LocalVarTargetImpl(targetType, table);
@@ -494,6 +498,8 @@ static LocalVarTarget ofVariable(TargetType targetType, List<LocalVarTargetInfo>
494498
/**
495499
* {@return a target for annotations on the type in a local variable declaration}
496500
* @param table the list of local variable targets
501+
* @throws IllegalArgumentException if the size of the list of targets
502+
* exceeds the limit of {@link java.lang.classfile##u2 u2}
497503
*/
498504
static LocalVarTarget ofLocalVariable(List<LocalVarTargetInfo> table) {
499505
return ofVariable(TargetType.LOCAL_VARIABLE, table);
@@ -503,6 +509,8 @@ static LocalVarTarget ofLocalVariable(List<LocalVarTargetInfo> table) {
503509
* {@return a target for annotations on the type in a local variable declared
504510
* as a resource in a try-with-resources statement}
505511
* @param table the list of local variable targets
512+
* @throws IllegalArgumentException if the size of the list of targets
513+
* exceeds the limit of {@link java.lang.classfile##u2 u2}
506514
*/
507515
static LocalVarTarget ofResourceVariable(List<LocalVarTargetInfo> table) {
508516
return ofVariable(TargetType.RESOURCE_VARIABLE, table);
@@ -802,7 +810,6 @@ sealed interface LocalVarTargetInfo
802810
*/
803811
Label startLabel();
804812

805-
806813
/**
807814
* The given local variable has a value at indices into the code array in the interval
808815
* [start_pc, start_pc + length), that is, between start_pc inclusive and start_pc + length exclusive.

src/java.base/share/classes/java/lang/classfile/attribute/CharacterRangeTableAttribute.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public sealed interface CharacterRangeTableAttribute
8888
* {@link CodeBuilder#characterRange CodeBuilder::characterRange} instead.
8989
*
9090
* @param ranges the descriptions of the character ranges
91+
* @throws IllegalArgumentException if the number of ranges exceeds the
92+
* limit of {@link java.lang.classfile##u2 u2}
9193
*/
9294
static CharacterRangeTableAttribute of(List<CharacterRangeInfo> ranges) {
9395
return new UnboundAttribute.UnboundCharacterRangeTableAttribute(ranges);

src/java.base/share/classes/java/lang/classfile/attribute/ExceptionsAttribute.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public sealed interface ExceptionsAttribute
7777
/**
7878
* {@return an {@code Exceptions} attribute}
7979
* @param exceptions the exceptions that may be thrown from this method
80+
* @throws IllegalArgumentException if the number of exceptions exceeds the
81+
* limit of {@link java.lang.classfile##u2 u2}
8082
*/
8183
static ExceptionsAttribute of(List<ClassEntry> exceptions) {
8284
return new UnboundAttribute.UnboundExceptionsAttribute(exceptions);
@@ -85,6 +87,8 @@ static ExceptionsAttribute of(List<ClassEntry> exceptions) {
8587
/**
8688
* {@return an {@code Exceptions} attribute}
8789
* @param exceptions the exceptions that may be thrown from this method
90+
* @throws IllegalArgumentException if the number of exceptions exceeds the
91+
* limit of {@link java.lang.classfile##u2 u2}
8892
*/
8993
static ExceptionsAttribute of(ClassEntry... exceptions) {
9094
return of(List.of(exceptions));
@@ -93,6 +97,8 @@ static ExceptionsAttribute of(ClassEntry... exceptions) {
9397
/**
9498
* {@return an {@code Exceptions} attribute}
9599
* @param exceptions the exceptions that may be thrown from this method
100+
* @throws IllegalArgumentException if the number of exceptions exceeds the
101+
* limit of {@link java.lang.classfile##u2 u2}
96102
*/
97103
static ExceptionsAttribute ofSymbols(List<ClassDesc> exceptions) {
98104
return of(Util.entryList(exceptions));
@@ -101,6 +107,8 @@ static ExceptionsAttribute ofSymbols(List<ClassDesc> exceptions) {
101107
/**
102108
* {@return an {@code Exceptions} attribute}
103109
* @param exceptions the exceptions that may be thrown from this method
110+
* @throws IllegalArgumentException if the number of exceptions exceeds the
111+
* limit of {@link java.lang.classfile##u2 u2}
104112
*/
105113
static ExceptionsAttribute ofSymbols(ClassDesc... exceptions) {
106114
return ofSymbols(Arrays.asList(exceptions));

src/java.base/share/classes/java/lang/classfile/attribute/InnerClassesAttribute.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public sealed interface InnerClassesAttribute
6565
/**
6666
* {@return an {@code InnerClasses} attribute}
6767
* @param innerClasses descriptions of the nested classes
68+
* @throws IllegalArgumentException if the number of descriptions exceeds
69+
* the limit of {@link java.lang.classfile##u2 u2}
6870
*/
6971
static InnerClassesAttribute of(List<InnerClassInfo> innerClasses) {
7072
return new UnboundAttribute.UnboundInnerClassesAttribute(innerClasses);
@@ -73,6 +75,8 @@ static InnerClassesAttribute of(List<InnerClassInfo> innerClasses) {
7375
/**
7476
* {@return an {@code InnerClasses} attribute}
7577
* @param innerClasses descriptions of the nested classes
78+
* @throws IllegalArgumentException if the number of descriptions exceeds
79+
* the limit of {@link java.lang.classfile##u2 u2}
7680
*/
7781
static InnerClassesAttribute of(InnerClassInfo... innerClasses) {
7882
return new UnboundAttribute.UnboundInnerClassesAttribute(List.of(innerClasses));

0 commit comments

Comments
 (0)