From db21685181b512107ebc752fbb06fece151c10d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= Date: Thu, 17 May 2018 02:35:32 +0200 Subject: [PATCH] GfxFont: Reject invalid names from encoding table Some PDF files contain invalid glyph names in the Encoding /Differences array. Putting these e.g. in a Type42 /Encoding array when converting it to postscript creates an invalid file, rejected by e.g. ghostscript. --- poppler/GfxFont.cc | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/poppler/GfxFont.cc b/poppler/GfxFont.cc index 7b4d4b54..847e3f19 100644 --- a/poppler/GfxFont.cc +++ b/poppler/GfxFont.cc @@ -944,6 +944,29 @@ static GBool testForNumericNames(Dict *fontDict, GBool hex) { return numeric; } +// Return gTrue if the name is valid, according to +// https://github.com/adobe-type-tools/agl-specification +static GBool glyphNameIsValid(const char *name) +{ + // Digit is invalid for the first character + if (isdigit(*name)) { + return gFalse; + } + + // Valid characters: [a-zA-Z0-9_.] + while (*name != '\0') { + if (islower(*name) || isdigit(*name) || + isupper(*name) || (*name == '_') || + (*name == '.')) { + name++; + } else { + return gFalse; + } + } + + return gTrue; +} + Gfx8BitFont::Gfx8BitFont(XRef *xref, const char *tagA, Ref idA, GooString *nameA, GfxFontType typeA, Ref embFontIDA, Dict *fontDict): GfxFont(tagA, idA, nameA, typeA, embFontIDA) { @@ -1221,7 +1244,7 @@ Gfx8BitFont::Gfx8BitFont(XRef *xref, const char *tagA, Ref idA, GooString *nameA if (obj3.isInt()) { code = obj3.getInt(); } else if (obj3.isName()) { - if (code >= 0 && code < 256) { + if (glyphNameIsValid(obj3.getName()) && code >= 0 && code < 256) { if (encFree[code]) { gfree(enc[code]); } -- 2.16.3