diff --git a/packages/deparser/__tests__/pretty/show-quoting.test.ts b/packages/deparser/__tests__/pretty/show-quoting.test.ts index 8d987063..52b689b5 100644 --- a/packages/deparser/__tests__/pretty/show-quoting.test.ts +++ b/packages/deparser/__tests__/pretty/show-quoting.test.ts @@ -26,3 +26,21 @@ 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 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); + } finally { + constructor.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); }