From b631b7fb74b3045b92172f383dccc952c66dcd60 Mon Sep 17 00:00:00 2001 From: iliev-blnk Date: Wed, 28 Oct 2026 15:40:29 +0200 Subject: [PATCH] Fix crashes in AAT contextual and ligature subtables (#150) Follow HarfBuzz in three cases where morx processing threw: - Contextual substitution without SetMark substitutes the first glyph processed instead of reading the mark at null (#150, MORX-19) - A ligature action that needs more components than are on the stack leaves the glyphs as they are and clears the stack, instead of popping undefined. Components are deleted only once their ligature is made (MORX-41) - Reading AAT arrays outside the font returns null instead of throwing a RangeError, and a ligature action stops there (MORX-41) The test fonts are MORX-18 and MORX-41 from Unicode text-rendering-tests, under the Apache License 2.0 in test/data/fonttest/LICENSE.txt. --- CHANGELOG.md | 1 + src/aat/AATMorxProcessor.js | 41 ++++++++++++++++++--- src/tables/aat.js | 10 +++++- test/data/fonttest/TestMORXEighteen.ttf | Bin 0 -> 3256 bytes test/data/fonttest/TestMORXFourtyone.ttf | Bin 0 -> 2248 bytes test/issues.js | 44 +++++++++++++++++++++++ 6 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 test/data/fonttest/TestMORXEighteen.ttf create mode 100644 test/data/fonttest/TestMORXFourtyone.ttf 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 0000000000000000000000000000000000000000..c6e6343ae78c357cf6eb36e525bb40c7ebccebb4 GIT binary patch literal 3256 zcmb7GeQZAc=Mdf(@=G%#j%))&%bH6R}W&rsyc@}PQ-i0>kj(~;3o*!-in{tVd%Xx<1IrWt7`=m%)EkyvWV_OvvP^{-&=ipEcb z*8imxG%phJSZFFiv-A|?RhTQW_{5ZQZ1yZsNde~XoesrD+lG&tiHhFAnvWClWQw1< z_YVO63gl-=U`N~ivn?BsmAC$lBxB$Nn>|2e;j&}RSq!QQy-cvrt%E~zq@~*f3m3YLd%{_tqq^TLkQsv!dDqp z0xvO_&1#WNR?(}fxx(f3xb3QHHERaNoafJW?SCVk`uDGg4+NilcJ7J5p^=yP!l7e( zgC&KV=R(i$h0ML?))x2gGw*ioYxN^`SN89`kN=#i5Wj4-*ff{VD$8n(-Q)IpHJ7c@ zWLKFr{nhD%BXx&czH`0*)u?auOSOl4`Ps|gIQPnyzVfnxndT zwRPzYv5T3zOwHV99=;IxO`S0UhGcaQM}F*TjV$7_X_8i< zI#iRo!(sQR7L$pk@5JMG5(&Kzy?Oljo1t9)So7N*?yFh{lbvM+rB2_n5>%U(wV=z} zGQru|@6R}_Ta+1;`$|MkR8kvqYRh4%F|cj)icR|*)J9`7MUpHg$I?!2^tiFHsH8O@ zeye`4`;GLGp}=e1T3v(N|Gn_R0g0^yx=1ULvN4aN#m{cKTDq;3+iR4qnL9mQm&BK{ z60`e}In<-jT%ssykX5}{)HM@15=(^-csDtl+k0N#yR*J+?M!}iay&A%S+hOfe|N3w=axMPRGAKedEf$wej$_@srVn=i2L;J737rmmirU zNwZ?>a&1i%Gnw1BZr#rOHw3=tJQsa=-YZe*tE$}L(<(Jdl`6a*%~DyJzZ)-VSKS?N zH{bk0X+fEDI?vnu>@2&P2{M;&|}G)lX`=?3Qn)zcv6Nvt?X z0Xl?#cIUEdqOC$TmR{v>D-`2^?`ED7Bpa?p!0fZPe)m6m(+EGTqX zUz-G1_*RhBdc7$taOHbl!V;oqf$o%^zo*d)&u1_fC=@-)F7nfWPN%2^qu9R`Fty-R zZP2qJ_B8=s5?%#{D8@Kq8G}54eDp4_NgAs}J>=q#BlZbobacgF IT}~VR103nnn*aa+ literal 0 HcmV?d00001 diff --git a/test/data/fonttest/TestMORXFourtyone.ttf b/test/data/fonttest/TestMORXFourtyone.ttf new file mode 100644 index 0000000000000000000000000000000000000000..be18c759254a4ba1d0f722c22741ff070248539d GIT binary patch literal 2248 zcmb7GUrd`-6hHTVUt3VwXlV-$LcX>RCWDT(r9~twU2%g^3LC;2F@=w|YtcdpEdq;K z78eu4Os3I9<8YZKOY~uHYJ4!6kIQ(_r)5ST(5P8_Fec7rKG6ERU)yO7xOtoV-E+=8 ze}3oOeiOfa^J9>K!814e4}jPF(QTybeQMgEo8De!^W4d5*dX5ZW7qh+pm24)zt3Z zKS+{pLqa#tyrzip)_dPHle2JFuHpB<8Z_8q0yfg^LN}4@Bq!PA8qJfKDK~>AG0-T< znux)+nRKAzB^60J%HDvaim&kqWJAS~MajhkdQG{cTQQ7>2ZPi{3dJM^7_I~Jbts5j zh~43$k_xgtNm1dKA0#JF(m2~BpR6Y*5RXGcTL6q^tedbj1Lr=BdW-@qycY`K6;ca@ z!uN#_G%X0wEclJmIl9Ueb{@J>#Ql@sfF}b{oy}<5YqS}o>|#E_Quz&jW6SI2@vU`e zqLyrv+o+LR;Kyn)I$9liy~Ezppfc^p?IuB#vQ$5&6s{G;^-lRpv& z1R?`R-{gM#(?+}V{My>3q851Glz?{=fxb0|SSiv^KQ% z9^3k8yEJ97KH3Qr>(fie9%hYb=ahIw?mI3f`*quNZdRnc^NQ!2x5p@>C(bd>dUzg|_)=a;Y=v@@%RVBKP%zT1^ zxCs|%6;Wp?Nh!+V^nQ%)qE0%9+fKpBpT}GbdT~lnJ0ydU#Bhf_3qA{rVeGOK6rm%~ zS%Rk={AHk%fHVQGOTef?{$PFOe_M3?%VG?XvOt%@sL)|VRdL%VwCE}1k%d&?oYyiP z19b}2vW72#xuQl|%Qyk8ENtVDFM$^MFM`g&yU-m&B|;cSP(h*lPtTzW4+>3awpmC; zYz4J-YF$B9%D5_{ma(Yx641?RHJrg$L|z6jP~fK3q6X-=M(3ypqgcfpF!j&@{6sax z>MlT*MO1+yjWGjh67wUdN9evywqzxCMKS;4n#REWZBvOfd*4e(DK8Z<6`Ik$sy4=O zr)sV6DfZTO4etPUWs6UqYF>0{6^xX5pRx|E4Q>#))DV7 y{sW+kcM5MWG9^@;=y|Gw9|>M`c)!Yx!A}XN5S`WJm$k3IP{?0H2UCm%yT1UJ8jFVj literal 0 HcmV?d00001 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'); + }); + }); });