diff --git a/src/erlang/BeamParser.ts b/src/erlang/BeamParser.ts index 07721cd726a..e68fdbc5706 100644 --- a/src/erlang/BeamParser.ts +++ b/src/erlang/BeamParser.ts @@ -179,7 +179,12 @@ const parseAtoms = ( return { atoms, trailingBytes: payload.length - at }; }; -const frameRows = (payload: Buffer, width: number, label: string) => { +const frameRows = ( + payload: Buffer | undefined, + width: number, + label: string, +) => { + if (payload === undefined) return null; if (payload.length < 4) failTruncated(`BEAM ${label} table is truncated.`); const reader = new BinaryReader(payload, false, "beam"); const count = reader.u32(0); @@ -191,9 +196,11 @@ const frameRows = (payload: Buffer, width: number, label: string) => { }; const mapRows = ( - { reader, count, rowBytes }: ReturnType, + table: ReturnType, project: (reader: BinaryReader, start: number) => Row, -): Row[] => { +): Row[] | null => { + if (table === null) return null; + const { reader, count, rowBytes } = table; const rows: Row[] = []; for (let index = 0; index < count; index += 1) rows.push(project(reader, 4 + index * rowBytes)); @@ -257,22 +264,15 @@ const buildErlangBeamReport = ( ? null : parseAtoms(atomChunk.payload, input.max_atoms, atomFormat); const atoms = atomTable?.atoms ?? null; - const exportChunk = payloads.get("ExpT"); - const importChunk = payloads.get("ImpT"); - const lambdaChunk = payloads.get("FunT"); - const exportTable = - exportChunk === undefined - ? null - : frameRows(exportChunk.payload, 3, "export"); - const importTable = - importChunk === undefined - ? null - : frameRows(importChunk.payload, 3, "import"); + const exportTable = frameRows(payloads.get("ExpT")?.payload, 3, "export"); + const importTable = frameRows(payloads.get("ImpT")?.payload, 3, "import"); // OTP skips the optional lambda parser for a zero-byte selected chunk. - const lambdaTable = - lambdaChunk === undefined || lambdaChunk.payload.length === 0 - ? null - : frameRows(lambdaChunk.payload, 6, "lambda"); + const lambdaPayload = payloads.get("FunT")?.payload; + const lambdaTable = frameRows( + lambdaPayload?.length === 0 ? undefined : lambdaPayload, + 6, + "lambda", + ); const tableRows = (exportTable?.count ?? 0) + (importTable?.count ?? 0) + @@ -287,48 +287,39 @@ const buildErlangBeamReport = ( required: tableRows, }, ); - const exports = - exportTable === null - ? null - : mapRows(exportTable, (reader, start) => { - const functionIndex = reader.u32(start); - return { - function_index: functionIndex, - function: atomAt(atoms, functionIndex), - arity: reader.u32(start + 4), - label: reader.u32(start + 8), - }; - }); - const imports = - importTable === null - ? null - : mapRows(importTable, (reader, start) => { - const moduleIndex = reader.u32(start); - const functionIndex = reader.u32(start + 4); - return { - module_index: moduleIndex, - module: atomAt(atoms, moduleIndex), - function_index: functionIndex, - function: atomAt(atoms, functionIndex), - arity: reader.u32(start + 8), - }; - }); + const exports = mapRows(exportTable, (reader, start) => { + const functionIndex = reader.u32(start); + return { + function_index: functionIndex, + function: atomAt(atoms, functionIndex), + arity: reader.u32(start + 4), + label: reader.u32(start + 8), + }; + }); + const imports = mapRows(importTable, (reader, start) => { + const moduleIndex = reader.u32(start); + const functionIndex = reader.u32(start + 4); + return { + module_index: moduleIndex, + module: atomAt(atoms, moduleIndex), + function_index: functionIndex, + function: atomAt(atoms, functionIndex), + arity: reader.u32(start + 8), + }; + }); // A four-byte zero count remains a decoded, explicitly empty table. - const lambdas = - lambdaTable === null - ? null - : mapRows(lambdaTable, (reader, start) => { - const functionIndex = reader.u32(start); - return { - function_index: functionIndex, - function: atomAt(atoms, functionIndex), - arity: reader.u32(start + 4), - label: reader.u32(start + 8), - index: reader.u32(start + 12), - free_vars: reader.u32(start + 16), - old_unique: reader.u32(start + 20), - }; - }); + const lambdas = mapRows(lambdaTable, (reader, start) => { + const functionIndex = reader.u32(start); + return { + function_index: functionIndex, + function: atomAt(atoms, functionIndex), + arity: reader.u32(start + 4), + label: reader.u32(start + 8), + index: reader.u32(start + 12), + free_vars: reader.u32(start + 16), + old_unique: reader.u32(start + 20), + }; + }); const codeChunk = payloads.get("Code"); const code = codeChunk === undefined