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 f0f8c91a08 VBAMacroReader: fall back to windows-1252 for unsupported 
project code pages (#1519)
f0f8c91a08 is described below

commit f0f8c91a084f4875bd2107b9b3d0a98f731d96b1
Author: nikhil1511 <[email protected]>
AuthorDate: Mon Nov 2 18:23:51 2026 +0530

    VBAMacroReader: fall back to windows-1252 for unsupported project code 
pages (#1519)
    
    When PROJECT_CODEPAGE specifies a code page that CodePageUtil or the JVM
    cannot resolve, log a warning and keep ModuleMap's existing WIN_1252
    default instead of aborting macro extraction, matching how MAPIMessage
    and HPSF Property handle unsupported code pages.
    
    Closes #1515
    
    Generated-by: Google Gemini
---
 .../apache/poi/poifs/macros/VBAMacroReader.java    | 10 +++++-
 .../poi/poifs/macros/TestVBAMacroReader.java       | 38 ++++++++++++++++++++++
 2 files changed, 47 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 de571ed125..eca36d5823 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
@@ -26,6 +26,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
@@ -540,7 +541,14 @@ public class VBAMacroReader implements Closeable {
                         case PROJECT_CODEPAGE:
                             in.readInt();//record size must == 4
                             int codepage = in.readShort();
-                            modules.charset = 
Charset.forName(CodePageUtil.codepageToEncoding(codepage, true));
+                            try {
+                                modules.charset = 
Charset.forName(CodePageUtil.codepageToEncoding(codepage, true));
+                            } catch (UnsupportedEncodingException | 
IllegalArgumentException e) {
+                                LOGGER.atWarn().log(
+                                        "Unsupported VBA project code page {}, 
decoding MBCS strings as windows-1252",
+                                        box(codepage));
+                                modules.charset = WIN_1252;
+                            }
                             break;
                         case MODULE_STREAM_NAME:
                             ASCIIUnicodeStringPair pair = readStringPair(in, 
modules.charset, STREAMNAME_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 cc53c58706..d4fe57ef28 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
@@ -571,6 +571,44 @@ class TestVBAMacroReader {
         }
     }
 
+    @Test
+    void unsupportedProjectCodepageFallsBackToWindows1252() throws IOException 
{
+        final String source = "Attribute VB_Name = \"Module1\"\r\nSub 
Run()\r\nMsgBox \"\u00e9\"\r\nEnd Sub\r\n";
+        final byte[] sourceBytes = source.getBytes(StringUtil.WIN_1252);
+        final byte[] asciiName = "Module1".getBytes(StandardCharsets.US_ASCII);
+        final byte[] utf16Name = "Module1".getBytes(StandardCharsets.UTF_16LE);
+
+        // Test both an unknown positive code page 
(UnsupportedCharsetException)
+        // and code page 0 (UnsupportedEncodingException from CodePageUtil).
+        for (int unsupportedCodepage : new int[]{10025, 0}) {
+            final ByteArrayOutputStream dir = new ByteArrayOutputStream();
+            writeShort(dir, 0x0003);
+            writeInt(dir, 2);
+            writeShort(dir, unsupportedCodepage);
+            writeShort(dir, 0x001A);
+            writeInt(dir, asciiName.length);
+            dir.writeBytes(asciiName);
+            writeShort(dir, 0x0032);
+            writeInt(dir, utf16Name.length);
+            dir.writeBytes(utf16Name);
+            writeShort(dir, 0x0031);
+            writeInt(dir, 4);
+            writeInt(dir, 0);
+            writeShort(dir, 0x0010);
+            writeInt(dir, 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("Module1", new 
ByteArrayInputStream(compressLiterals(sourceBytes)));
+
+                Map<String, String> macros = reader.readMacros();
+                assertEquals(Collections.singletonMap("Module1", source), 
macros);
+            }
+        }
+    }
+
     /**
      * RLE-compresses {@code data} into a single chunk of literals only
      */


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

Reply via email to