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 2021/03/10 03:57:41 UTC

svn commit: r1887405 - /pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/OS2WindowsMetricsTable.java

Author: tilman
Date: Wed Mar 10 03:57:41 2021
New Revision: 1887405

URL: http://svn.apache.org/viewvc?rev=1887405&view=rev
Log:
PDFBOX-5123, PDFBOX-5124: gracefully recover from EOF

Modified:
    pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/OS2WindowsMetricsTable.java

Modified: pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/OS2WindowsMetricsTable.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/OS2WindowsMetricsTable.java?rev=1887405&r1=1887404&r2=1887405&view=diff
==============================================================================
--- pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/OS2WindowsMetricsTable.java (original)
+++ pdfbox/branches/2.0/fontbox/src/main/java/org/apache/fontbox/ttf/OS2WindowsMetricsTable.java Wed Mar 10 03:57:41 2021
@@ -16,18 +16,28 @@
  */
 package org.apache.fontbox.ttf;
 
+import java.io.EOFException;
 import java.io.IOException;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 /**
- * A table in a true type font.
- * 
+ * The OS/2 and Windows Metrics Table in a TrueType font, see
+ * <a href="https://docs.microsoft.com/en-us/typography/opentype/spec/os2">here</a>.
+ *
  * @author Ben Litchfield
- * 
+ *
  */
 public class OS2WindowsMetricsTable extends TTFTable
 {
 
     /**
+     * Log instance.
+     */
+    private static final Log LOG = LogFactory.getLog(OS2WindowsMetricsTable.class);
+
+    /**
      * Weight class constant.
      */
     public static final int WEIGHT_CLASS_THIN = 100;
@@ -819,16 +829,36 @@ public class OS2WindowsMetricsTable exte
         winDescent = data.readUnsignedShort();
         if (version >= 1)
         {
-            codePageRange1 = data.readUnsignedInt();
-            codePageRange2 = data.readUnsignedInt();
+            try
+            {
+                codePageRange1 = data.readUnsignedInt();
+                codePageRange2 = data.readUnsignedInt();
+            }
+            catch (EOFException ex)
+            {
+                version = 0;
+                LOG.warn("Could not read all expected parts of version >= 1, setting version to 0", ex);
+                initialized = true;
+                return;
+            }
         }
-        if (version >= 1.2)
+        if (version >= 2)
         {
-            sxHeight = data.readSignedShort();
-            sCapHeight = data.readSignedShort();
-            usDefaultChar = data.readUnsignedShort();
-            usBreakChar = data.readUnsignedShort();
-            usMaxContext = data.readUnsignedShort();
+            try
+            {
+                sxHeight = data.readSignedShort();
+                sCapHeight = data.readSignedShort();
+                usDefaultChar = data.readUnsignedShort();
+                usBreakChar = data.readUnsignedShort();
+                usMaxContext = data.readUnsignedShort();
+            }
+            catch (EOFException ex)
+            {
+                version = 1;
+                LOG.warn("Could not read all expected parts of version >= 2, setting version to 1", ex);
+                initialized = true;
+                return;
+            }
         }
         initialized = true;
     }