{"record":{"id":"7122d14916fca3b3","repo":"can1357/oh-my-pi","slug":"archive-is-too-large-to-read-safely-7122d1","errorCode":null,"errorMessage":"Archive is too large to read safely","messagePattern":"Archive is too large to read safely","errorType":"exception","errorClass":"ArchiveError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/ar/source.ts","lineNumber":45,"sourceCode":"\treturn buffer.subarray(start, end);\n}\n\n/** Wrap borrowed bytes as a {@link ByteSource}. */\nexport function memoryByteSource(buffer: Uint8Array): ByteSource {\n\treturn {\n\t\tsize: buffer.byteLength,\n\t\tasync read(start, end) {\n\t\t\treturn readMemoryRange(buffer, start, end);\n\t\t},\n\t};\n}\n\n/** Lazily read ranges of a file on disk as a {@link ByteSource}. */\nexport function fileByteSource(filePath: string): ByteSource {\n\tconst file = Bun.file(filePath);\n\tconst size = file.size;\n\tif (!Number.isSafeInteger(size)) {\n\t\tthrow new ArchiveError(\"Archive is too large to read safely\");\n\t}\n\treturn {\n\t\tsize,\n\t\tasync read(start, end) {\n\t\t\tassertValidRange(start, end);\n\t\t\tconst bytes = await file.slice(start, end).bytes();\n\t\t\tif (bytes.byteLength !== end - start) {\n\t\t\t\tthrow new ArchiveError(\"Invalid archive: truncated data\");\n\t\t\t}\n\t\t\treturn bytes;\n\t\t},\n\t};\n}\n\n/** Materialize an entire {@link ByteSource}; use only under a limits check. */\nexport async function readAllBytes(source: ByteSource): Promise<Uint8Array> {\n\treturn source.read(0, source.size);\n}","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/ar/source.ts#L27-L63","documentation":"`fileByteSource` refuses to back a ByteSource with a file whose size is not a JS safe integer (i.e. larger than 2^53-1 bytes). Such sizes cannot be handled with exact integer range arithmetic, so the library fails fast instead of silently producing wrong offsets.","triggerScenarios":"Calling `fileByteSource` (or `resolveSource` with a file path) on a file whose reported size exceeds Number.MAX_SAFE_INTEGER — ~9 petabytes. Practically impossible for real disks; only reachable via exotic filesystems reporting bogus sizes.","commonSituations":"Virtual/synthetic filesystems or FUSE mounts reporting overflowed sizes; corrupted stat results; tests mocking Bun.file with string/NaN sizes that fail the safe-integer check.","solutions":["Check the file's real size with `Bun.file(path).size` before opening and reject if not a safe integer","If on a FUSE/virtual mount, verify the size reporting is correct at the mount layer","For test setups, ensure mocked file objects expose a numeric, safe-integer `size`"],"exampleFix":"// before: opening directly\nconst src = fileByteSource(mountPath);\n// after: pre-check the reported size\nconst f = Bun.file(mountPath);\nif (!Number.isSafeInteger(f.size)) throw new Error(`unusable size for ${mountPath}: ${f.size}`);\nconst src = fileByteSource(mountPath);","handlingStrategy":"validation","validationCode":"const f = Bun.file(path);\nif (!Number.isSafeInteger(f.size)) throw new Error(`file size not representable: ${f.size}`);","typeGuard":"function hasSafeSize(size: unknown): size is number {\n\treturn typeof size === \"number\" && Number.isSafeInteger(size);\n}","tryCatchPattern":"try {\n\tconst src = fileByteSource(path);\n} catch (err) {\n\tif (err instanceof ArchiveError && err.message.includes(\"too large to read safely\")) {\n\t\tthrow new Error(`cannot open ${path}: size exceeds safe integer range`);\n\t}\n\tthrow err;\n}","preventionTips":["Check Bun.file(path).size before opening archives on unusual mounts","Distrust FUSE/virtual filesystems that report overflowed sizes","In tests, ensure mocked file objects expose numeric safe-integer sizes"],"tags":["archive","file-size","limits","validation"],"backgroundTag":"file-too-large","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}