Skip to content

Commit 8425c36

Browse files
committed
fix(pyinstaller): attribute malformed TOC rows at their owner
1 parent f6a1e2c commit 8425c36

3 files changed

Lines changed: 56 additions & 23 deletions

File tree

‎docs/python-packaged-applications.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ they do not claim a destination directory. Their `relative_offset` and
4444
`compressed_size` preserve any payload declared by older producers, while
4545
`declared_size` is zero because no bytecode is materialized for a namespace.
4646
Every declared span must end before the PYZ table of contents.
47+
Malformed CArchive TOC rows identify their zero-based table index and byte offset
48+
within the TOC, including truncated headers, invalid names, compression flags,
49+
and type codes. The failure retains its original format reason.
4750
PYZ `compression_flag` is derived from storage: nonempty payloads are compressed,
4851
including legacy namespaces; an empty namespace has no compressed payload.
4952

‎src/artifacts/pyinstaller/CArchiveLayout.ts‎

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ export const parseCArchiveToc = (
137137
): Result<readonly CArchiveEntry[], PyInstallerFailure> => {
138138
const entries: CArchiveEntry[] = [];
139139
let offset = 0;
140+
const malformedEntry = (message: string): Result<never, PyInstallerFailure> =>
141+
err(
142+
pyInstallerFailure(
143+
"format",
144+
`CArchive table index ${String(entries.length)} at TOC offset ${String(offset)}: ${message}`,
145+
),
146+
);
140147
while (offset < bytes.length) {
141148
if (entries.length >= MAX_TOC_ENTRIES)
142149
return err(
@@ -146,42 +153,26 @@ export const parseCArchiveToc = (
146153
),
147154
);
148155
if (offset + TOC_HEADER_BYTES > bytes.length)
149-
return err(
150-
pyInstallerFailure(
151-
"format",
152-
"CArchive table of contents entry is truncated",
153-
),
154-
);
156+
return malformedEntry("CArchive table of contents entry is truncated");
155157
const entryLength = bytes.readUInt32BE(offset);
156158
if (entryLength < TOC_HEADER_BYTES || offset + entryLength > bytes.length)
157-
return err(
158-
pyInstallerFailure(
159-
"format",
160-
"CArchive table of contents entry extends outside the table",
161-
),
159+
return malformedEntry(
160+
"CArchive table of contents entry extends outside the table",
162161
);
163162
const nameBytes = bytes.subarray(
164163
offset + TOC_HEADER_BYTES,
165164
offset + entryLength,
166165
);
167166
const name = tocName(nameBytes);
168-
if (!name.ok) return name;
167+
if (!name.ok) return malformedEntry(name.error.message);
169168
const compressionFlag = bytes[offset + 16] ?? 0;
170169
if (compressionFlag !== 0 && compressionFlag !== 1)
171-
return err(
172-
pyInstallerFailure(
173-
"format",
174-
`CArchive compression flag ${String(compressionFlag)} is not stored or zlib`,
175-
),
170+
return malformedEntry(
171+
`CArchive compression flag ${String(compressionFlag)} is not stored or zlib`,
176172
);
177173
const typeCode = bytes[offset + 17] ?? 0;
178174
if (typeCode < 0x20 || typeCode > 0x7e)
179-
return err(
180-
pyInstallerFailure(
181-
"format",
182-
"CArchive type code is not a printable character",
183-
),
184-
);
175+
return malformedEntry("CArchive type code is not a printable character");
185176
entries.push({
186177
index: entries.length,
187178
tocRecordOffset: offset,

‎src/artifacts/pyinstaller/PyInstallerFormatBoundaries.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,45 @@ import {
2626
type ArtifactEntry,
2727
} from "../ArtifactReader.js";
2828

29+
it.each([0, 1, 2])(
30+
"identifies malformed CArchive compression at table index %i",
31+
async (index) => {
32+
const original = cArchiveExecutable({
33+
layout: "2.1",
34+
pythonVersion: 309,
35+
members: ["a", "b", "c"].map((name) => ({
36+
name,
37+
typeCode: "x",
38+
payload: Buffer.from(name),
39+
compress: false,
40+
})),
41+
});
42+
const toc = 64 + original.readUInt32BE(original.length - 88 + 12);
43+
let record = toc;
44+
for (let row = 0; row < index; row++)
45+
record += original.readUInt32BE(record);
46+
await withPyInstallerReader(original, async (reader) => {
47+
const entries = [];
48+
for await (const entry of reader.entries()) entries.push(entry);
49+
expect(entries).toHaveLength(3);
50+
});
51+
const malformed = Buffer.from(original);
52+
malformed[record + 16] = 2;
53+
await expect(
54+
withPyInstallerReader(malformed, async (reader) => {
55+
for await (const _entry of reader.entries()) {
56+
/* force TOC admission */
57+
}
58+
}),
59+
).rejects.toMatchObject({
60+
reason: "format",
61+
message: expect.stringContaining(
62+
`table index ${index} at TOC offset ${record - toc}`,
63+
),
64+
});
65+
},
66+
);
67+
2968
it.each([
3069
["empty", Buffer.alloc(0)],
3170
["short", Buffer.from("630000", "hex")],

0 commit comments

Comments
 (0)