This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-bcel.git
The following commit(s) were added to refs/heads/master by this push:
new 2c1a5fc0 Handle unrecognized tags in Attribute.toString()
2c1a5fc0 is described below
commit 2c1a5fc0c3d0b220696dba8eb93e7b6a92fc2fcc
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Nov 20 09:39:26 2026 -0500
Handle unrecognized tags in Attribute.toString()
Use the declared attribute name for tags outside the known range,
preserving standard names for recognized tags.
Add regression coverage for registered custom readers, tag boundaries,
reader cleanup, and existing Unknown formatting. Document the fallback.
---
src/changes/changes.xml | 1 +
.../java/org/apache/bcel/classfile/Attribute.java | 7 +-
.../org/apache/bcel/classfile/AttributeTest.java | 79 ++++++++++++++++++++++
3 files changed, 86 insertions(+), 1 deletion(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 821494ea..8e7ba364 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -65,6 +65,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="hataehoon,
Gary Gregory">Escape unknown method attribute names in Class2HTML code
reports.</action>
<action type="fix" dev="ggregory" due-to="Gary
Gregory">Fix Class2HTML ArrayIndexOutOfBoundsException for unknown Code
sub-attributes and render their descriptions with HTML escaping.</action>
<action type="fix" dev="ggregory" due-to="Gary
Gregory">Reject class names containing '#' or '%' before generating Class2HTML
reports, and encode report links for accepted names containing spaces,
ampersands, and other special characters.</action>
+ <action type="fix" dev="ggregory" due-to="Gary
Gregory">Avoid ArrayIndexOutOfBoundsException in Attribute.toString() for
custom attributes with unrecognized tags.</action>
<!-- ADD -->
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary
Gregory">Bump org.apache.commons:commons-lang3 from 3.21.0 to 3.22.0.</action>
diff --git a/src/main/java/org/apache/bcel/classfile/Attribute.java
b/src/main/java/org/apache/bcel/classfile/Attribute.java
index e1017cbd..25939197 100644
--- a/src/main/java/org/apache/bcel/classfile/Attribute.java
+++ b/src/main/java/org/apache/bcel/classfile/Attribute.java
@@ -464,10 +464,15 @@ public abstract class Attribute implements Cloneable,
Node {
}
/**
+ * Returns the standard name for a known attribute tag, or the declared
name for an unrecognized tag.
+ * <p>
+ * Custom attributes can use {@link Const#ATTR_UNKNOWN} and inherit this
implementation. Their names are obtained from {@link #getName()}.
+ * </p>
+ *
* @return attribute name.
*/
@Override
public String toString() {
- return Const.getAttributeName(tag);
+ return tag >= 0 && tag < Const.KNOWN_ATTRIBUTES ?
Const.getAttributeName(tag) : getName();
}
}
diff --git a/src/test/java/org/apache/bcel/classfile/AttributeTest.java
b/src/test/java/org/apache/bcel/classfile/AttributeTest.java
index f89c3354..f8ca7766 100644
--- a/src/test/java/org/apache/bcel/classfile/AttributeTest.java
+++ b/src/test/java/org/apache/bcel/classfile/AttributeTest.java
@@ -19,18 +19,44 @@
package org.apache.bcel.classfile;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
+import java.io.IOException;
+import org.apache.bcel.Const;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
/**
* Tests {@link Attribute}.
*/
public class AttributeTest {
+ /**
+ * Minimal custom attribute that deliberately inherits the base {@link
Attribute#toString()} implementation.
+ */
+ private static final class TestAttribute extends Attribute {
+
+ private TestAttribute(final byte tag, final int nameIndex, final int
length, final ConstantPool constantPool) {
+ super(tag, nameIndex, length, constantPool);
+ }
+
+ @Override
+ public void accept(final Visitor visitor) {
+ // This custom attribute has no visitor callback or payload.
+ }
+
+ @Override
+ public Attribute copy(final ConstantPool constantPool) {
+ return new TestAttribute(getTag(), getNameIndex(), getLength(),
constantPool);
+ }
+ }
+
@Test
public void testReadAttributeInvalidName() {
final DataInputStream dataInput = new DataInputStream(new
ByteArrayInputStream(new byte[] { 0, 0, 0, 0, 0 }));
@@ -46,4 +72,57 @@ public class AttributeTest {
final ConstantPool constantPool = new ConstantPool(constants);
assertThrows(ClassFormatException.class, () ->
Attribute.readAttribute(dataInput, constantPool));
}
+
+ /**
+ * Formats a custom attribute parsed through a registered reader without
requiring a subclass {@code toString()} override.
+ * <p>
+ * The serialized attribute has a custom name and an empty payload. Its
reader returns an attribute with {@link Const#ATTR_UNKNOWN}, which used
+ * to make the inherited method index the standard-name table at -1. The
reader is removed even if an assertion fails; reparsing then checks that
+ * the built-in {@link Unknown} attribute still uses its existing
description.
+ * </p>
+ *
+ * @throws IOException if the attribute cannot be parsed.
+ */
+ @Test
+ public void testToStringForRegisteredCustomAttribute() throws IOException {
+ final String name = AttributeTest.class.getName() + ".CustomAttribute";
+ final ConstantPool constantPool = new ConstantPool(new Constant[] {
null, new ConstantUtf8(name) });
+ final byte[] bytes = { 0, 1, 0, 0, 0, 0 }; // name_index = 1,
attribute_length = 0.
+ Attribute.addAttributeReader(name, (UnknownAttributeReader)
(nameIndex, length, input, pool) ->
+ new TestAttribute(Const.ATTR_UNKNOWN, nameIndex, length, pool));
+ try (DataInputStream input = new DataInputStream(new
ByteArrayInputStream(bytes))) {
+ final TestAttribute attribute =
assertInstanceOf(TestAttribute.class, Attribute.readAttribute(input,
constantPool));
+ assertEquals(Const.ATTR_UNKNOWN, attribute.getTag());
+ assertEquals(name, attribute.toString());
+ } finally {
+ Attribute.removeAttributeReader(name);
+ }
+ try (DataInputStream input = new DataInputStream(new
ByteArrayInputStream(bytes))) {
+ final Unknown attribute = assertInstanceOf(Unknown.class,
Attribute.readAttribute(input, constantPool));
+ assertEquals("(Unknown attribute " + name + ")",
attribute.toString());
+ }
+ }
+
+ /**
+ * Preserves the standard names at both ends of the known-tag range even
when the constant-pool name differs.
+ */
+ @Test
+ public void testToStringForKnownTags() {
+ final ConstantPool constantPool = new ConstantPool(new Constant[] {
null, new ConstantUtf8("DifferentName") });
+ assertEquals("SourceFile", new TestAttribute(Const.ATTR_SOURCE_FILE,
1, 0, constantPool).toString());
+ assertEquals("RuntimeInvisibleTypeAnnotations", new
TestAttribute(Const.ATTR_RUNTIME_INVISIBLE_TYPE_ANNOTATIONS, 1, 0,
constantPool).toString());
+ }
+
+ /**
+ * Uses the declared name for negative tags, including {@link
Const#ATTR_UNKNOWN}, and tags above the standard-name table.
+ *
+ * @param tag an unrecognized attribute tag.
+ */
+ @ParameterizedTest
+ @ValueSource(ints = { Byte.MIN_VALUE, Const.ATTR_UNKNOWN,
Const.KNOWN_ATTRIBUTES, Byte.MAX_VALUE })
+ public void testToStringForUnrecognizedTags(final int tag) {
+ final String name = "CustomAttribute";
+ final ConstantPool constantPool = new ConstantPool(new Constant[] {
null, new ConstantUtf8(name) });
+ assertEquals(name, new TestAttribute((byte) tag, 1, 0,
constantPool).toString());
+ }
}