From 87142a635e9da6d4b394d2521675a0cb4cbc55c2 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 31 Mar 2020 23:10:15 +0200 Subject: [PATCH] Ensure that `Font.charToGlyph` won't fail because `String.fromCodePoint` is given an invalid code point (issue 11768) *Please note:* This patch on its own is *not* sufficient to address the underlying problem in the referenced issue, hence why no test-case is included since the *actual* bug still needs to be fixed. As can be seen in the specification, https://tc39.es/ecma262/#sec-string.fromcodepoint, `String.fromCodePoint` will throw a RangeError for invalid code points. In the event that a CMap, in a composite font, contains invalid data and/or we fail to parse it correctly, it's thus possible that the glyph mapping that we build end up with entires that cause `String.fromCodePoint` to throw and thus `Font.charToGlyph` to break. If that happens, as is the case in issue 11768, significant portions of a page/document may fail to render which seems very unfortunate. While this patch doesn't fix the underlying problem, it's hopefully deemed useful not only for the referenced issue but also to prevent similar bugs in the future. --- src/core/fonts.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/core/fonts.js b/src/core/fonts.js index c21387c45..e5f6be7d4 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -3216,10 +3216,14 @@ var Font = (function FontClosure() { }; } - var fontChar = - typeof fontCharCode === "number" - ? String.fromCodePoint(fontCharCode) - : ""; + let fontChar = ""; + if (typeof fontCharCode === "number") { + if (fontCharCode <= 0x10ffff) { + fontChar = String.fromCodePoint(fontCharCode); + } else { + warn(`charToGlyph - invalid fontCharCode: ${fontCharCode}`); + } + } var glyph = this.glyphCache[charcode]; if (