Skip to content

Commit 31d5411

Browse files
committed
PDFBOX-6066: early return when unplausible data
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1928344 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0697231 commit 31d5411

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

fontbox/src/main/java/org/apache/fontbox/ttf/GlyphSubstitutionTable.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ private Map<String, ScriptTable> readScriptList(TTFDataStream data, long offset)
161161
{
162162
scriptTags[i] = data.readString(4);
163163
scriptOffsets[i] = data.readUnsignedShort();
164+
if (scriptOffsets[i] < data.getCurrentPosition() - offset)
165+
{
166+
// can't be before the current position
167+
LOG.error("scriptOffsets[{}]: {} implausible: data.getCurrentPosition() - offset = {}",
168+
i, scriptOffsets[i], data.getCurrentPosition() - offset);
169+
return Collections.unmodifiableMap(resultScriptList);
170+
}
164171
}
165172
for (int i = 0; i < scriptCount; i++)
166173
{
@@ -180,15 +187,22 @@ private ScriptTable readScriptTable(TTFDataStream data, long offset) throws IOEx
180187
for (int i = 0; i < langSysCount; i++)
181188
{
182189
langSysTags[i] = data.readString(4);
183-
if (i > 0 && langSysTags[i].compareTo(langSysTags[i-1]) <= 0)
190+
langSysOffsets[i] = data.readUnsignedShort();
191+
if (langSysOffsets[i] < data.getCurrentPosition() - offset)
192+
{
193+
// can't be before the current position
194+
LOG.error("langSysOffsets[{}]: {} implausible: data.getCurrentPosition() - offset = {}",
195+
i, langSysOffsets[i], data.getCurrentPosition() - offset);
196+
return new ScriptTable(null, new LinkedHashMap<>());
197+
}
198+
if (i > 0 && langSysTags[i].compareTo(langSysTags[i-1]) < 0)
184199
{
185200
// PDFBOX-4489: catch corrupt file
186201
// https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#slTbl_sRec
187-
LOG.error("LangSysRecords not alphabetically sorted by LangSys tag: {} <= {}",
202+
LOG.error("LangSysRecords not alphabetically sorted by LangSys tag: {} < {}",
188203
langSysTags[i], langSysTags[i - 1]);
189204
return new ScriptTable(null, new LinkedHashMap<>());
190205
}
191-
langSysOffsets[i] = data.readUnsignedShort();
192206
}
193207

194208
LangSysTable defaultLangSysTable = null;
@@ -350,10 +364,12 @@ private LookupTable readLookupTable(TTFDataStream data, long offset) throws IOEx
350364
{
351365
LOG.error("subTableOffsets[{}] is 0 at offset {}", i,
352366
data.getCurrentPosition() - 2);
367+
return new LookupTable(lookupType, lookupFlag, 0, new LookupSubTable[0]);
353368
}
354-
else if (offset + subTableOffsets[i] > data.getOriginalDataSize())
369+
if (offset + subTableOffsets[i] > data.getOriginalDataSize())
355370
{
356371
LOG.error("{} > {}", offset + subTableOffsets[i], data.getOriginalDataSize());
372+
return new LookupTable(lookupType, lookupFlag, 0, new LookupSubTable[0]);
357373
}
358374
}
359375

0 commit comments

Comments
 (0)