refactor(managed): derive inspection from PE CLI extents

This commit is contained in:
morluto
2026-09-29 17:35:41 +08:00
parent 3b64c813cf
commit dfad09e3b5
34 changed files with 192 additions and 626 deletions
+10 -10
View File
@@ -101,12 +101,12 @@ const compressedLength = (
);
};
/** Read one bounded UTF-8 value from #Strings. */
/** Read one UTF-8 value from #Strings within its PE/CLI stream extent. */
export const readMetadataString = (
bytes: Buffer,
layout: ManagedMetadataLayout,
index: number,
maxBytes: number,
heapExtent: number,
): string => {
if (index === 0) return "";
if (index < 0 || index >= layout.strings.size)
@@ -119,15 +119,15 @@ export const readMetadataString = (
const start = layout.strings.offset + index;
const maximum = Math.min(
layout.strings.offset + layout.strings.size,
start + maxBytes + 1,
start + heapExtent + 1,
);
let end = start;
while (end < maximum && bytes[end] !== 0) end += 1;
if (end === maximum)
throw managedFailure(
"limit-exceeded",
"invalid-string",
"metadata.#Strings",
`String heap item exceeds max_heap_item_bytes ${String(maxBytes)}`,
"String heap item has no terminator within #Strings",
start,
);
try {
@@ -144,12 +144,12 @@ export const readMetadataString = (
}
};
/** Read one bounded item from #Blob without interpreting its contents. */
/** Read one #Blob item while validating its encoded extent against the heap. */
export const readMetadataBlob = (
bytes: Buffer,
layout: ManagedMetadataLayout,
index: number,
maxBytes: number,
heapExtent: number,
): Buffer => {
if (index === 0) return Buffer.alloc(0);
if (index < 0 || index >= layout.blob.size)
@@ -161,11 +161,11 @@ export const readMetadataBlob = (
);
const start = layout.blob.offset + index;
const decoded = compressedLength(bytes, start, "metadata.#Blob");
if (decoded.length > maxBytes)
if (decoded.length > heapExtent)
throw managedFailure(
"limit-exceeded",
"invalid-blob",
"metadata.#Blob",
`Blob heap item exceeds max_heap_item_bytes ${String(maxBytes)}`,
"Blob length exceeds the admitted heap extent",
start,
);
const content = start + decoded.prefix;