This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new 84883941ab Review "Keep UniqueAttributesImpl tracking in step with its 
attributes"
84883941ab is described below

commit 84883941ab908625abe22e1db3c4172874e55243
Author: Mark Thomas <[email protected]>
AuthorDate: Fri Nov 6 14:36:20 2026 +0000

    Review "Keep UniqueAttributesImpl tracking in step with its attributes"
    
    Add rejection of bad indexes
    
    Co-authored by: Claude / Sonnet 5.5
---
 .../apache/jasper/util/UniqueAttributesImpl.java   | 10 ++++++
 .../jasper/util/TestUniqueAttributesImpl.java      | 41 ++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/java/org/apache/jasper/util/UniqueAttributesImpl.java 
b/java/org/apache/jasper/util/UniqueAttributesImpl.java
index aa809f50ad..8127d38dea 100644
--- a/java/org/apache/jasper/util/UniqueAttributesImpl.java
+++ b/java/org/apache/jasper/util/UniqueAttributesImpl.java
@@ -92,6 +92,7 @@ public class UniqueAttributesImpl extends AttributesImpl {
 
     @Override
     public void setAttribute(int index, String uri, String localName, String 
qName, String type, String value) {
+        checkIndex(index);
         String oldQName = super.getQName(index);
         qNames.remove(oldQName);
         if (qNames.add(qName)) {
@@ -113,6 +114,7 @@ public class UniqueAttributesImpl extends AttributesImpl {
 
     @Override
     public void setQName(int index, String qName) {
+        checkIndex(index);
         String oldQName = super.getQName(index);
         qNames.remove(oldQName);
         if (qNames.add(qName)) {
@@ -126,6 +128,14 @@ public class UniqueAttributesImpl extends AttributesImpl {
         }
     }
 
+    private void checkIndex(int index) {
+        // Reject a bad index before touching the tracking set, so that a
+        // failed call leaves it in step with the attributes
+        if (index < 0 || index >= getLength()) {
+            throw new ArrayIndexOutOfBoundsException("Attempt to modify 
attribute at illegal index: " + index);
+        }
+    }
+
     private void handleDuplicate(String qName, String value) {
         handleDuplicate(qName, value, this);
     }
diff --git a/test/org/apache/jasper/util/TestUniqueAttributesImpl.java 
b/test/org/apache/jasper/util/TestUniqueAttributesImpl.java
index 74f3c35215..451456de1c 100644
--- a/test/org/apache/jasper/util/TestUniqueAttributesImpl.java
+++ b/test/org/apache/jasper/util/TestUniqueAttributesImpl.java
@@ -270,4 +270,45 @@ public class TestUniqueAttributesImpl {
         }
         Assert.assertEquals(1, attrs.getLength());
     }
+
+
+    /*
+     * A call with an illegal index fails without side effects. It must not
+     * leave the new name in the tracking set, where it would cause a later
+     * attribute of that name to be rejected as a duplicate.
+     */
+    @Test
+    public void testIllegalIndexLeavesTrackingUnchanged() throws Exception {
+        UniqueAttributesImpl attrs = createAttributes();
+
+        try {
+            attrs.setQName(5, "c");
+            Assert.fail("Expected ArrayIndexOutOfBoundsException");
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // expected
+        }
+        try {
+            attrs.setAttribute(-1, "", "d", "d", "CDATA", "4");
+            Assert.fail("Expected ArrayIndexOutOfBoundsException");
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // expected
+        }
+        try {
+            attrs.setQName(5, "a");
+            Assert.fail("Expected ArrayIndexOutOfBoundsException");
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // expected
+        }
+
+        // The names are still free and the existing names are still tracked
+        attrs.addAttribute("", "c", "c", "CDATA", "3");
+        attrs.addAttribute("", "d", "d", "CDATA", "4");
+        Assert.assertEquals(4, attrs.getLength());
+        try {
+            attrs.addAttribute("", "a", "a", "CDATA", "5");
+            Assert.fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to