diff --git a/CHANGELOG.md b/CHANGELOG.md index 175e3382..feb2ee2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Unreleased - End the second curve of CFF hflex and hflex1 operators at the starting y, so later points are no longer shifted (#437) +- Fix crashes in AAT morx contextual substitution without a mark and ligatures with too few components or components outside the font, like HarfBuzz (#150) ### [3.0.0] - 11-11-2026 diff --git a/src/aat/AATMorxProcessor.js b/src/aat/AATMorxProcessor.js index ee68e244..a7e43fab 100644 --- a/src/aat/AATMorxProcessor.js +++ b/src/aat/AATMorxProcessor.js @@ -111,8 +111,11 @@ export default class AATMorxProcessor { return; } + let reverse = !!(this.subtable.coverage & REVERSE_DIRECTION); + this.ligatureStack = []; - this.markedGlyph = null; + // Without SetMark, the first glyph processed is the marked one + this.markedGlyph = reverse ? glyphs.length - 1 : 0; this.firstGlyph = null; this.lastGlyph = null; this.markedIndex = 0; @@ -121,7 +124,6 @@ export default class AATMorxProcessor { let stateMachine = this.getStateMachine(subtable); let process = this.getProcessor(); - let reverse = !!(this.subtable.coverage & REVERSE_DIRECTION); return stateMachine.process(this.glyphs, reverse, process, this.limits); } @@ -218,7 +220,7 @@ export default class AATMorxProcessor { this.ligatureStack.push(index); } - if (entry.flags & PERFORM_ACTION) { + if (entry.flags & PERFORM_ACTION && this.ligatureStack.length > 0) { let actions = this.subtable.table.ligatureActions; let components = this.subtable.table.components; let ligatureList = this.subtable.table.ligatureList; @@ -228,22 +230,48 @@ export default class AATMorxProcessor { let ligatureIndex = 0; let codePoints = []; let ligatureGlyphs = []; + // Components popped since the last ligature, deleted once a ligature + // replaces them + let popped = []; while (!last) { + if (this.ligatureStack.length === 0) { + // The action uses more components than there are on the stack. + // Like HarfBuzz, clear the stack and stop. + return; + } + let componentGlyph = this.ligatureStack.pop(); + popped.push(componentGlyph); codePoints.unshift(...this.glyphs[componentGlyph].codePoints); let action = actions.getItem(actionIndex++); + if (action == null) { + break; + } + last = !!(action & LAST_MASK); let store = !!(action & STORE_MASK); let offset = ((action & OFFSET_MASK) << 2) >> 2; // sign extend 30 to 32 bits offset += this.glyphs[componentGlyph].id; let component = components.getItem(offset); + if (component == null) { + break; + } + ligatureIndex += component; if (last || store) { let ligatureEntry = ligatureList.getItem(ligatureIndex); + if (ligatureEntry == null) { + break; + } + + for (let i = 0; i < popped.length - 1; i++) { + this.glyphs[popped[i]] = this.font.getGlyph(0xffff); + } + this.glyphs[componentGlyph] = this.font.getGlyph( ligatureEntry, codePoints, @@ -251,11 +279,14 @@ export default class AATMorxProcessor { ligatureGlyphs.push(componentGlyph); ligatureIndex = 0; codePoints = []; - } else { - this.glyphs[componentGlyph] = this.font.getGlyph(0xffff); + popped = []; } } + // If the action stopped at data outside the font, keep the components + // it didn't use on the stack. + this.ligatureStack.push(...popped.reverse()); + // Put ligature glyph indexes back on the stack this.ligatureStack.push(...ligatureGlyphs); } diff --git a/src/tables/aat.js b/src/tables/aat.js index b5a5aaab..33f621bc 100644 --- a/src/tables/aat.js +++ b/src/tables/aat.js @@ -9,10 +9,18 @@ class UnboundedArrayAccessor { this._items = []; } + // Returns null for items outside the font, which malformed or malicious + // fonts can point to. getItem(index) { + let size = this.type.size(null, this.parent); + let offset = this.base + size * index; + if (index < 0 || offset + size > this.stream.length) { + return null; + } + if (this._items[index] == null) { let pos = this.stream.pos; - this.stream.pos = this.base + this.type.size(null, this.parent) * index; + this.stream.pos = offset; this._items[index] = this.type.decode(this.stream, this.parent); this.stream.pos = pos; } diff --git a/test/data/fonttest/TestMORXEighteen.ttf b/test/data/fonttest/TestMORXEighteen.ttf new file mode 100644 index 00000000..c6e6343a Binary files /dev/null and b/test/data/fonttest/TestMORXEighteen.ttf differ diff --git a/test/data/fonttest/TestMORXFourtyone.ttf b/test/data/fonttest/TestMORXFourtyone.ttf new file mode 100644 index 00000000..be18c759 Binary files /dev/null and b/test/data/fonttest/TestMORXFourtyone.ttf differ diff --git a/test/issues.js b/test/issues.js index 28756502..b26ba1bf 100644 --- a/test/issues.js +++ b/test/issues.js @@ -495,4 +495,48 @@ describe('issues', function () { } }); }); + + describe('#150 - TypeError with AAT contextual substitution without a mark', function () { + // MORX-19 from Unicode text-rendering-tests substitutes the marked glyph + // without setting a mark, so the first glyph is marked. + let font = fontkit.openSync( + new URL('data/fonttest/TestMORXEighteen.ttf', import.meta.url), + ); + let layout = (text) => + font + .layout(text) + .glyphs.map((glyph) => glyph.name) + .join(' '); + + it('should substitute the first glyph', function () { + assert.equal(layout('ACDE'), 'A.alt C D.alt1 E'); + assert.equal(layout('D'), 'D.alt'); + }); + }); + + describe('AAT ligature stack underflow and overflow', function () { + // MORX-41 from Unicode text-rendering-tests makes a ligature of the last + // two components when it sees c, whatever is on the stack. + let font = fontkit.openSync( + new URL('data/fonttest/TestMORXFourtyone.ttf', import.meta.url), + ); + let layout = (text) => + font + .layout(text) + .glyphs.map((glyph) => glyph.name) + .join(' '); + + it('should make ligatures', function () { + assert.equal(layout('ac'), 'a_c'); + assert.equal(layout('bc'), 'b_c'); + }); + + it('should leave the glyphs when the stack has too few components', function () { + assert.equal(layout('cc'), 'c c'); + }); + + it('should stop at components outside the font', function () { + assert.equal(layout('abcc'), 'a b_c c'); + }); + }); });