You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2024/03/08 09:46:15 UTC

svn commit: r1916178 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/FileSystemFontProvider.java

Author: tilman
Date: Fri Mar  8 09:46:15 2024
New Revision: 1916178

URL: http://svn.apache.org/viewvc?rev=1916178&view=rev
Log:
PDFBOX-5781: avoid OOM that can happen with huge fonts, by Kim Hage

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/FileSystemFontProvider.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/FileSystemFontProvider.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/FileSystemFontProvider.java?rev=1916178&r1=1916177&r2=1916178&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/FileSystemFontProvider.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/FileSystemFontProvider.java Fri Mar  8 09:46:15 2024
@@ -303,7 +303,7 @@ final class FileSystemFontProvider exten
         String hash;
         try
         {
-            hash = computeHash(Files.readAllBytes(file.toPath()));
+            hash = computeHash(Files.newInputStream(file.toPath()));
         }
         catch (IOException ex)
         {
@@ -586,7 +586,7 @@ final class FileSystemFontProvider exten
                         // first check whether time is different and if yes, whether hash is different
                         if (fontFile.lastModified() != lastModified)
                         {
-                            String newHash = computeHash(Files.readAllBytes(fontFile.toPath()));
+                            String newHash = computeHash(Files.newInputStream(fontFile.toPath()));
                             if (newHash.equals(hash))
                             {
                                 keep = true;
@@ -720,7 +720,7 @@ final class FileSystemFontProvider exten
                     panose = os2WindowsMetricsTable.getPanose();
                 }
 
-                String hash = computeHash(ttf.getOriginalData().readAllBytes());
+                String hash = computeHash(ttf.getOriginalData());
 
                 String format;
                 if (ttf instanceof OpenTypeFont && ((OpenTypeFont) ttf).isPostScript())
@@ -808,7 +808,7 @@ final class FileSystemFontProvider exten
                 LOG.warn("Skipping font with '|' in name {} in file {}", type1.getName(), pfbFile);
                 return;
             }
-            String hash = computeHash(Files.readAllBytes(pfbFile.toPath()));
+            String hash = computeHash(Files.newInputStream(pfbFile.toPath()));
             fontInfoList.add(new FSFontInfo(pfbFile, FontFormat.PFB, type1.getName(),
                                             null, -1, -1, 0, 0, -1, null, this, hash, pfbFile.lastModified()));
 
@@ -843,11 +843,23 @@ final class FileSystemFontProvider exten
         return fontInfoList;
     }
 
-    private static String computeHash(byte[] ba)
+    // closes the input
+    // doesn't use readAllBytes() because some fonts are huge (PDFBOX-5781)
+    private static String computeHash(InputStream is) throws IOException
     {
         CRC32 crc = new CRC32();
-        crc.update(ba);
-        long l = crc.getValue();
-        return Long.toHexString(l);
+
+        try (is)
+        {
+            byte[] buffer = new byte[4096];
+            int readBytes;
+            while ((readBytes = is.read(buffer)) != -1)
+            {
+                crc.update(buffer, 0, readBytes);
+            }
+
+            long hash = crc.getValue();
+            return Long.toHexString(hash);
+        }
     }
 }