Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/tables/cff.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ function equals(a, b) {
}
}

// Maximum subroutine call depth as defined by the CFF/Type 2 specification (section 4.7).
// FreeType and other conforming implementations also enforce this limit.
const MAX_CALL_DEPTH = 10;

// Subroutines are encoded using the negative half of the number space.
// See type 2 chapter 4.7 "Subroutine operators".
function calcCFFSubroutineBias(subrs) {
Expand Down Expand Up @@ -636,6 +640,7 @@ function parseCFFCharstring(font, glyph, code, version, coords) {
let vsindex = 0;
let vstore = [];
let blendVector;
let callDepth = 0;
const cffTable = font.tables.cff2 || font.tables.cff;
defaultWidthX = cffTable.topDict._defaultWidthX;
nominalWidthX = cffTable.topDict._nominalWidthX;
Expand Down Expand Up @@ -775,7 +780,13 @@ function parseCFFCharstring(font, glyph, code, version, coords) {
codeIndex = stack.pop() + subrsBias;
subrCode = subrs[codeIndex];
if (subrCode) {
if (callDepth >= MAX_CALL_DEPTH) {
console.warn('CFF charstring subroutine call depth exceeded, skipping callsubr');
break;
}
callDepth++;
parse(subrCode);
callDepth--;
}

break;
Expand Down Expand Up @@ -1068,7 +1079,13 @@ function parseCFFCharstring(font, glyph, code, version, coords) {
codeIndex = stack.pop() + font.gsubrsBias;
subrCode = font.gsubrs[codeIndex];
if (subrCode) {
if (callDepth >= MAX_CALL_DEPTH) {
console.warn('CFF charstring subroutine call depth exceeded, skipping callgsubr');
break;
}
callDepth++;
parse(subrCode);
callDepth--;
}

break;
Expand Down
Binary file added test/fonts/CFFRecursionTest.otf
Binary file not shown.
Loading
Loading