From 779ed48ed6752d45dc189550930412e93f73953e Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Sat, 21 Nov 2026 03:46:57 +0000 Subject: [PATCH 1/2] fix(deparser): bound SHOW segment encoding allocations --- .../deparser/__tests__/pretty/show-quoting.test.ts | 13 +++++++++++++ packages/deparser/src/deparser.ts | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/deparser/__tests__/pretty/show-quoting.test.ts b/packages/deparser/__tests__/pretty/show-quoting.test.ts index 8d987063..e22029c1 100644 --- a/packages/deparser/__tests__/pretty/show-quoting.test.ts +++ b/packages/deparser/__tests__/pretty/show-quoting.test.ts @@ -26,3 +26,16 @@ it('rejects an overlong SHOW identifier that cannot be split', () => { expect(() => Deparser.deparse({ VariableShowStmt: { name: `${'x'.repeat(64)}..` } })) .toThrow('SHOW name cannot be split into identifiers of at most 63 bytes'); }); + +it('bounds encoding work for a SHOW name with many dot boundaries', () => { + const encode = jest.spyOn(TextEncoder.prototype, 'encode'); + const name = `${'a.'.repeat(100000)}.z`; + const started = Date.now(); + try { + expect(Deparser.deparse({ VariableShowStmt: { name } })).toMatch(/^SHOW /); + expect(Date.now() - started).toBeLessThan(10000); + expect(encode.mock.calls.every(([segment]) => segment!.length <= 63)).toBe(true); + } finally { + encode.mockRestore(); + } +}); diff --git a/packages/deparser/src/deparser.ts b/packages/deparser/src/deparser.ts index 352a6d6a..90ce5efd 100644 --- a/packages/deparser/src/deparser.ts +++ b/packages/deparser/src/deparser.ts @@ -4450,12 +4450,12 @@ export class Deparser implements DeparserVisitor { const boundaries = new Map(); for (let start = node.name.length - 1; start >= 0; start--) { if (start > 0 && node.name[start - 1] !== '.') continue; - if (encoder.encode(node.name.slice(start)).length <= 63) { + if (node.name.length - start <= 63 && encoder.encode(node.name.slice(start)).length <= 63) { boundaries.set(start, node.name.length); continue; } for (let end = start + 1; end < node.name.length - 1; end++) { - if (encoder.encode(node.name.slice(start, end)).length > 63) break; + if (end - start > 63 || encoder.encode(node.name.slice(start, end)).length > 63) break; if (node.name[end] === '.' && boundaries.has(end + 1)) { boundaries.set(start, end); } From ec41faca60abc5d21cbb78db259b7eee47c06107 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Sat, 21 Nov 2026 03:58:34 +0000 Subject: [PATCH 2/2] test(deparser): avoid recording millions of encoder calls --- .../deparser/__tests__/pretty/show-quoting.test.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/deparser/__tests__/pretty/show-quoting.test.ts b/packages/deparser/__tests__/pretty/show-quoting.test.ts index e22029c1..52b689b5 100644 --- a/packages/deparser/__tests__/pretty/show-quoting.test.ts +++ b/packages/deparser/__tests__/pretty/show-quoting.test.ts @@ -28,14 +28,19 @@ it('rejects an overlong SHOW identifier that cannot be split', () => { }); it('bounds encoding work for a SHOW name with many dot boundaries', () => { - const encode = jest.spyOn(TextEncoder.prototype, 'encode'); + const encoder = new TextEncoder(); + const constructor = jest.spyOn(globalThis, 'TextEncoder').mockImplementation(() => ({ + encode(segment: string) { + if (segment.length > 63) throw new Error('Unbounded SHOW encoding'); + return encoder.encode(segment); + } + } as TextEncoder)); const name = `${'a.'.repeat(100000)}.z`; const started = Date.now(); try { expect(Deparser.deparse({ VariableShowStmt: { name } })).toMatch(/^SHOW /); expect(Date.now() - started).toBeLessThan(10000); - expect(encode.mock.calls.every(([segment]) => segment!.length <= 63)).toBe(true); } finally { - encode.mockRestore(); + constructor.mockRestore(); } });