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

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 901eda24c7 VBAMacroReader: key module map by StreamNameUnicode (#1516)
901eda24c7 is described below

commit 901eda24c7e965a64b1819b8a6603dd9d96287ad
Author: nikhil1511 <[email protected]>
AuthorDate: Mon Nov 2 18:23:39 2026 +0530

    VBAMacroReader: key module map by StreamNameUnicode (#1516)
    
    Per [MS-OVBA] 2.3.4.2.3.2.3 and [MS-CFB] 2.6.1, StreamNameUnicode holds
    the UTF-16 stream name matching the compound file directory entry name.
    Using pair.unicode() (falling back to pair.ascii() only when empty)
    avoids a NullPointerException in readMacros() and preserves module type
    lookup via PROJECTwm when the MBCS StreamName does not round-trip
    through the project charset.
    
    Closes #1512
    
    Generated-by: Google Gemini
---
 .../apache/poi/poifs/macros/VBAMacroReader.java    |  4 +-
 .../poi/poifs/macros/TestVBAMacroReader.java       | 52 ++++++++++++++++++++++
 2 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java 
b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java
index 616626afc2..de571ed125 100644
--- a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java
+++ b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java
@@ -544,7 +544,9 @@ public class VBAMacroReader implements Closeable {
                             break;
                         case MODULE_STREAM_NAME:
                             ASCIIUnicodeStringPair pair = readStringPair(in, 
modules.charset, STREAMNAME_RESERVED);
-                            streamName = pair.ascii();
+                            // [MS-OVBA] 2.3.4.2.3.2.3: StreamNameUnicode MUST 
contain the UTF-16 encoding
+                            // of StreamName, matching the UTF-16 compound 
file entry name ([MS-CFB] 2.6.1).
+                            streamName = pair.unicode().isEmpty() ? 
pair.ascii() : pair.unicode();
                             break;
                         case PROJECT_DOC_STRING:
                             readStringPair(in, modules.charset, 
DOC_STRING_RESERVED);
diff --git 
a/poi/src/test/java/org/apache/poi/poifs/macros/TestVBAMacroReader.java 
b/poi/src/test/java/org/apache/poi/poifs/macros/TestVBAMacroReader.java
index 26a5c67fe1..cc53c58706 100644
--- a/poi/src/test/java/org/apache/poi/poifs/macros/TestVBAMacroReader.java
+++ b/poi/src/test/java/org/apache/poi/poifs/macros/TestVBAMacroReader.java
@@ -503,6 +503,58 @@ class TestVBAMacroReader {
         }
     }
 
+    @Test
+    void moduleMapKeyedByStreamNameUnicode() throws IOException {
+        final String unicodeStreamName = "\u9ad9\u6a4b\u2460";
+        final String source = "Attribute VB_Name = \"Mod1\"\r\nSub 
Run()\r\nEnd Sub\r\n";
+        // Simulate an MBCS StreamName that does not round-trip to the UTF-16 
entry name
+        final byte[] mbcsName = "???".getBytes(StandardCharsets.US_ASCII);
+        final byte[] utf16Name = 
unicodeStreamName.getBytes(StandardCharsets.UTF_16LE);
+
+        final ByteArrayOutputStream dir = new ByteArrayOutputStream();
+        // PROJECT_CODEPAGE = 1252
+        writeShort(dir, 0x0003);
+        writeInt(dir, 2);
+        writeShort(dir, 1252);
+        // MODULE_STREAM_NAME (MBCS + 0x0032 + UTF-16LE)
+        writeShort(dir, 0x001A);
+        writeInt(dir, mbcsName.length);
+        dir.writeBytes(mbcsName);
+        writeShort(dir, 0x0032);
+        writeInt(dir, utf16Name.length);
+        dir.writeBytes(utf16Name);
+        // MODULE_OFFSET = 0
+        writeShort(dir, 0x0031);
+        writeInt(dir, 4);
+        writeInt(dir, 0);
+        // DIR_STREAM_TERMINATOR
+        writeShort(dir, 0x0010);
+        writeInt(dir, 0);
+
+        final ByteArrayOutputStream projectWm = new ByteArrayOutputStream();
+        projectWm.writeBytes(mbcsName);
+        projectWm.write(0);
+        projectWm.writeBytes(utf16Name);
+        projectWm.write(new byte[]{0, 0, 0, 0});
+
+        try (POIFSFileSystem fs = new POIFSFileSystem();
+             VBAMacroReader reader = new VBAMacroReader(fs)) {
+            DirectoryNode vba = (DirectoryNode) 
fs.getRoot().createDirectory("VBA");
+            vba.createDocument("dir", new 
ByteArrayInputStream(compressLiterals(dir.toByteArray())));
+            vba.createDocument(unicodeStreamName, new ByteArrayInputStream(
+                    
compressLiterals(source.getBytes(StandardCharsets.US_ASCII))));
+            fs.getRoot().createDocument("PROJECTwm", new 
ByteArrayInputStream(projectWm.toByteArray()));
+            fs.getRoot().createDocument("PROJECT", new ByteArrayInputStream(
+                    "Module=???\r\n".getBytes(StandardCharsets.US_ASCII)));
+
+            Map<String, String> macros = reader.readMacros();
+            assertEquals(Collections.singletonMap(unicodeStreamName, source), 
macros);
+
+            Map<String, Module> modules = reader.readMacroModules();
+            assertEquals(Module.ModuleType.Module, 
modules.get(unicodeStreamName).geModuleType());
+        }
+    }
+
     /**
      * a dir stream that fails to decompress is reported as an IOException
      */


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

Reply via email to