Author: fanningpj
Date: Wed Jun 24 12:13:28 2026
New Revision: 1935616

Log:
guard out-of-range string code in StringPool.stringForCode. Thanks to aizu-m. 
This closes #70

Added:
   
xmlbeans/trunk/src/test/java/org/apache/xmlbeans/impl/schema/StringPoolCodeTest.java
Modified:
   
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java

Modified: 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java
==============================================================================
--- 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java
      Wed Jun 24 12:08:27 2026        (r1935615)
+++ 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java
      Wed Jun 24 12:13:28 2026        (r1935616)
@@ -692,6 +692,9 @@ public class SchemaTypeSystemImpl extend
         }
 
         String stringForCode(int code) {
+            if (code < 0 || code >= intsToStrings.size()) {
+                throw new SchemaTypeLoaderException("String code " + code + " 
out of range", _name, _handle, 
SchemaTypeLoaderException.UNRECOGNIZED_INDEX_ENTRY);
+            }
             return code == 0 ? null : intsToStrings.get(code);
         }
 

Added: 
xmlbeans/trunk/src/test/java/org/apache/xmlbeans/impl/schema/StringPoolCodeTest.java
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ 
xmlbeans/trunk/src/test/java/org/apache/xmlbeans/impl/schema/StringPoolCodeTest.java
        Wed Jun 24 12:13:28 2026        (r1935616)
@@ -0,0 +1,46 @@
+/*   Copyright 2026 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xmlbeans.impl.schema;
+
+import org.apache.xmlbeans.SchemaTypeLoaderException;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class StringPoolCodeTest {
+
+    @Test
+    void stringForCodeRejectsOutOfRange() {
+        SchemaTypeSystemImpl.StringPool pool = new 
SchemaTypeSystemImpl.StringPool("handle", "name");
+        int code = pool.codeForString("abc");
+
+        assertNull(pool.stringForCode(0));
+        assertEquals("abc", pool.stringForCode(code));
+
+        // a code past the end of the pool, as read from a crafted .xsb, must
+        // surface as the documented loader exception, not 
IndexOutOfBoundsException
+        assertThrows(SchemaTypeLoaderException.class, () -> 
pool.stringForCode(code + 1));
+    }
+
+    @Test
+    void stringForCodeRejectsNegative() {
+        SchemaTypeSystemImpl.StringPool pool = new 
SchemaTypeSystemImpl.StringPool("handle", "name");
+        // readUnsignedShortOrInt() falls back to a signed readInt() for the 
0xffff
+        // marker, so a negative code can reach stringForCode
+        assertThrows(SchemaTypeLoaderException.class, () -> 
pool.stringForCode(-1));
+    }
+}


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

Reply via email to