pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1859501 - in /pdfbox/trunk/fontbox/src: main/java/org/apache/fontbox/cmap/CMapParser.java test/java/org/apache/fontbox/cmap/TestCMapParser.java test/resources/cmap/CMapMalformedbfrange1 test/resources/cmap/CMapMalformedbfrange2
Date Sun, 19 May 2019 13:53:09 GMT
Author: lehmi
Date: Sun May 19 13:53:09 2019
New Revision: 1859501

URL: http://svn.apache.org/viewvc?rev=1859501&view=rev
Log:
PDFBOX-4550: add missing check for the start and end values of a bfrange

Added:
    pdfbox/trunk/fontbox/src/test/resources/cmap/CMapMalformedbfrange1
    pdfbox/trunk/fontbox/src/test/resources/cmap/CMapMalformedbfrange2
Modified:
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cmap/CMapParser.java
    pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/cmap/TestCMapParser.java

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cmap/CMapParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cmap/CMapParser.java?rev=1859501&r1=1859500&r2=1859501&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cmap/CMapParser.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/cmap/CMapParser.java Sun May 19
13:53:09 2019
@@ -356,10 +356,10 @@ public class CMapParser
             }
             byte[] startCode = (byte[]) nextToken;
             byte[] endCode = (byte[]) parseNextToken(cmapStream);
-            if (startCode.length != endCode.length)
+            if (!checkBoundaries(startCode, endCode))
             {
                 // PDFBOX-4550: likely corrupt stream
-                continue;
+                break;
             }
             nextToken = parseNextToken(cmapStream);
             List<byte[]> array = null;
@@ -412,6 +412,15 @@ public class CMapParser
         }
     }
 
+    private boolean checkBoundaries(byte[] startCode, byte[] endCode)
+    {
+        int start = CMap.toInt(startCode, startCode.length);
+        int end = CMap.toInt(endCode, endCode.length);
+        // end has to be bigger than start or equal
+        // the range can not represent more that 255 values
+        return end >= start && (end - start) < 256;
+    }
+
     /**
      * Returns an input stream containing the given "use" CMap.
      *
@@ -708,7 +717,7 @@ public class CMapParser
         return intValue;
     }
 
-    private String createStringFromBytes(byte[] bytes) throws IOException
+    private String createStringFromBytes(byte[] bytes)
     {
         return new String(bytes, bytes.length == 1 ? Charsets.ISO_8859_1 : Charsets.UTF_16BE);
     }

Modified: pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/cmap/TestCMapParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/cmap/TestCMapParser.java?rev=1859501&r1=1859500&r2=1859501&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/cmap/TestCMapParser.java (original)
+++ pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/cmap/TestCMapParser.java Sun May
19 13:53:09 2019
@@ -110,4 +110,37 @@ public class TestCMapParser extends Test
 
         assertNotNull("Failed to parse nasty CMap file", cMap);
     }
+
+    public void testParserWithMalformedbfrange1() throws IOException
+    {
+        CMap cMap = new CMapParser()
+                .parse(new File("src/test/resources/cmap", "CMapMalformedbfrange1"));
+
+        assertNotNull("Failed to parse malformed CMap file", cMap);
+
+        byte[] bytes1 = { 0, 1 };
+        assertEquals("bytes 00 01 from bfrange <0001> <0009> <0041>", "A",
+                cMap.toUnicode(toInt(bytes1, bytes1.length)));
+
+        byte[] bytes2 = { 1, 00 };
+        assertNull(cMap.toUnicode(toInt(bytes2, bytes2.length)));
+
+    }
+
+    public void testParserWithMalformedbfrange2() throws IOException
+    {
+        CMap cMap = new CMapParser()
+                .parse(new File("src/test/resources/cmap", "CMapMalformedbfrange2"));
+
+        assertNotNull("Failed to parse malformed CMap file", cMap);
+
+        byte[] bytes1 = { 0, 1 };
+        assertEquals("bytes 00 01 from bfrange <0001> <0009> <0041>", "A",
+                cMap.toUnicode(toInt(bytes1, bytes1.length)));
+
+        byte[] bytes2 = { 2, 00 };
+        assertNull(cMap.toUnicode(toInt(bytes2, bytes2.length)));
+
+    }
+
 }

Added: pdfbox/trunk/fontbox/src/test/resources/cmap/CMapMalformedbfrange1
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/test/resources/cmap/CMapMalformedbfrange1?rev=1859501&view=auto
==============================================================================
--- pdfbox/trunk/fontbox/src/test/resources/cmap/CMapMalformedbfrange1 (added)
+++ pdfbox/trunk/fontbox/src/test/resources/cmap/CMapMalformedbfrange1 Sun May 19 13:53:09
2019
@@ -0,0 +1,32 @@
+%% Licensed to the Apache Software Foundation (ASF) under one or more
+%% contributor license agreements.  See the NOTICE file distributed with
+%% this work for additional information regarding copyright ownership.
+%% The ASF licenses this file to You 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.
+%%
+%% This file isn't a valid CMap file, but it contains everything what
+%% is needed to test the CMapParser
+%%
+%%EndComments
+
+1 begincodespacerange<0000> <02FF>
+endcodespacerange
+
+2 beginbfrange
+<0001> <0009> <0041>
+<0109> <0100> <0030>
+endbfrange
+
+endcmap
+
+%%EndResource
+%%EOF

Added: pdfbox/trunk/fontbox/src/test/resources/cmap/CMapMalformedbfrange2
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/test/resources/cmap/CMapMalformedbfrange2?rev=1859501&view=auto
==============================================================================
--- pdfbox/trunk/fontbox/src/test/resources/cmap/CMapMalformedbfrange2 (added)
+++ pdfbox/trunk/fontbox/src/test/resources/cmap/CMapMalformedbfrange2 Sun May 19 13:53:09
2019
@@ -0,0 +1,32 @@
+%% Licensed to the Apache Software Foundation (ASF) under one or more
+%% contributor license agreements.  See the NOTICE file distributed with
+%% this work for additional information regarding copyright ownership.
+%% The ASF licenses this file to You 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.
+%%
+%% This file isn't a valid CMap file, but it contains everything what
+%% is needed to test the CMapParser
+%%
+%%EndComments
+
+1 begincodespacerange<0000> <02FF>
+endcodespacerange
+
+2 beginbfrange
+<0001> <0009> <0041>
+<0200> <0400> <0030>
+endbfrange
+
+endcmap
+
+%%EndResource
+%%EOF



Mime
View raw message