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 cd934840 Fix Class2HTML ArrayIndexOutOfBoundsException for unknown 
Code sub-attributes
cd934840 is described below

commit cd934840929d91816532c99a2ff0e7de9fa20870
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Nov 19 21:17:20 2026 -0500

    Fix Class2HTML ArrayIndexOutOfBoundsException for unknown Code
    sub-attributes
    
    Unknown attributes carry tag Const.ATTR_UNKNOWN (-1), which
    CodeHTML.writeMethod()
    passed unguarded to Const.getAttributeName() while rendering Code-level
    sub-attributes, so generating a report for any method whose Code
    attribute has
    an unknown sub-attribute crashed with ArrayIndexOutOfBoundsException.
    
    Guard the tag like the method-attribute loop and render the Unknown
    description with the shared Class2HTML.toHTML() escaping. Add a
    regression
    test that round-trips such a class through the parser and asserts the
    escaped description is shown.
---
 src/changes/changes.xml                            |  1 +
 src/main/java/org/apache/bcel/util/CodeHTML.java   |  8 +++-
 .../org/apache/bcel/util/Class2HTMLXSSTest.java    | 48 ++++++++++++++++++++++
 3 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b8e64941..853f59f8 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -63,6 +63,7 @@ The <action> type attribute can be add,update,fix,remove.
       <!-- FIX -->
       <action                  type="fix" dev="ggregory" due-to="Naveed Khan, 
Gary Gregory">InstConstraintVisitor check objectref class in getfield and 
putfield verification (#547).</action>
       <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>
       <!-- 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/util/CodeHTML.java 
b/src/main/java/org/apache/bcel/util/CodeHTML.java
index 47f2502a..71ab7ceb 100644
--- a/src/main/java/org/apache/bcel/util/CodeHTML.java
+++ b/src/main/java/org/apache/bcel/util/CodeHTML.java
@@ -530,8 +530,12 @@ final class CodeHTML {
                     printWriter.print("<UL>");
                     for (int j = 0; j < attributes2.length; j++) {
                         tag = attributes2[j].getTag();
-                        printWriter.print("<LI><A HREF=\"" + className + 
"_attributes.html#method" + methodNumber + "@" + i + "@" + j
-                            + "\" TARGET=Attributes>" + 
Const.getAttributeName(tag) + "</A></LI>\n");
+                        if (tag != Const.ATTR_UNKNOWN) {
+                            printWriter.print("<LI><A HREF=\"" + className + 
"_attributes.html#method" + methodNumber + "@" + i + "@" + j
+                                + "\" TARGET=Attributes>" + 
Const.getAttributeName(tag) + "</A></LI>\n");
+                        } else {
+                            printWriter.print("<LI>" + 
Class2HTML.toHTML(String.valueOf(attributes2[j])) + "</LI>\n");
+                        }
                     }
                     printWriter.print("</UL>");
                 }
diff --git a/src/test/java/org/apache/bcel/util/Class2HTMLXSSTest.java 
b/src/test/java/org/apache/bcel/util/Class2HTMLXSSTest.java
index 1b20a373..3899a1f8 100644
--- a/src/test/java/org/apache/bcel/util/Class2HTMLXSSTest.java
+++ b/src/test/java/org/apache/bcel/util/Class2HTMLXSSTest.java
@@ -28,10 +28,14 @@ import java.io.File;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.Arrays;
 
 import org.apache.bcel.Const;
+import org.apache.bcel.classfile.Attribute;
 import org.apache.bcel.classfile.ClassParser;
+import org.apache.bcel.classfile.Code;
 import org.apache.bcel.classfile.JavaClass;
+import org.apache.bcel.classfile.Method;
 import org.apache.bcel.classfile.Unknown;
 import org.apache.bcel.generic.ClassGen;
 import org.apache.bcel.generic.ConstantPoolGen;
@@ -129,4 +133,48 @@ class Class2HTMLXSSTest {
         assertTrue(code.contains("<LI>(Unknown attribute x&lt;svg 
onload=&quot;alert(1)&quot;&gt;&amp;&#39;&quot;y" + expectedPayload + ")</LI>"),
             "expected the complete unknown attribute description to be 
HTML-escaped");
     }
+
+    /**
+     * Tests that an unknown code-level attribute description is shown with 
its actual (escaped) name.
+     *
+     * <p>
+     * Prior to the fix, the attribute tag was passed to {@code 
Const.getAttributeName()} unconditionally, which threw
+     * {@code ArrayIndexOutOfBoundsException} for {@code Const.ATTR_UNKNOWN} 
(-1), so Class2HTML crashed on any method whose Code attribute
+     * has an unknown sub-attribute.
+     * </p>
+     */
+    @Test
+    void testUnknownCodeAttributeNameIsShown(@TempDir final Path outputDir) 
throws Exception {
+        final ClassGen cg = new ClassGen("EvilUnknownCodeAttr", 
"java.lang.Object", "EvilUnknownCodeAttr.java", Const.ACC_PUBLIC, null);
+        final ConstantPoolGen cpg = cg.getConstantPool();
+        final String marker = "x<script>alert(1)</script>";
+        final int nameIndex = cpg.addUtf8(marker);
+        final InstructionList instructions = new InstructionList();
+        instructions.append(InstructionConst.ICONST_0);
+        instructions.append(InstructionConst.IRETURN);
+        final MethodGen mg = new MethodGen(Const.ACC_PUBLIC | 
Const.ACC_STATIC, Type.INT, Type.NO_ARGS, null, "value", "EvilUnknownCodeAttr", 
instructions, cpg);
+        mg.setMaxStack();
+        mg.setMaxLocals();
+        final Method method = mg.getMethod();
+        for (final Attribute attribute : method.getAttributes()) {
+            if (attribute instanceof Code) {
+                final Code code = (Code) attribute;
+                final Attribute[] subAttributes = code.getAttributes();
+                final Attribute[] extended = Arrays.copyOf(subAttributes, 
subAttributes.length + 1);
+                extended[subAttributes.length] = new Unknown(nameIndex, 0, new 
byte[0], cpg.getConstantPool());
+                code.setAttributes(extended);
+            }
+        }
+        cg.addMethod(method);
+        instructions.dispose();
+
+        final JavaClass jc = new ClassParser(new 
ByteArrayInputStream(cg.getJavaClass().getBytes()), 
"EvilUnknownCodeAttr.class").parse();
+        assertNotNull(jc.getMethods()[0].getCode(), "expected the parsed 
method to retain its Code attribute");
+        new Class2HTML(jc, outputDir.toAbsolutePath() + File.separator);
+
+        final String codeHtml = new 
String(Files.readAllBytes(outputDir.resolve("EvilUnknownCodeAttr_code.html")), 
StandardCharsets.UTF_8);
+        assertFalse(codeHtml.contains(">SourceFile</A>"), "unknown code 
attribute was mislabeled as SourceFile");
+        assertTrue(codeHtml.contains("<LI>(Unknown attribute 
x&lt;script&gt;alert(1)&lt;/script&gt;)</LI>"),
+            "expected the unknown code attribute description to be 
HTML-escaped");
+    }
 }

Reply via email to