diff --git a/demo/web/batch.mjs b/demo/web/batch.mjs index 5058dbea4f..1899532f98 100644 --- a/demo/web/batch.mjs +++ b/demo/web/batch.mjs @@ -6,6 +6,7 @@ function safeSize(value) { } export function memoryEstimate(archiveBytes, selectedBytes, libraryBytes, entries) { + for (const value of [archiveBytes, selectedBytes, libraryBytes, entries]) safeSize(value); return safeSize(Math.ceil(archiveBytes * 1.02) + selectedBytes * 4 + libraryBytes + entries * 1024 + 8 * 1024 * 1024); } diff --git a/demo/web/tests/batch.test.mjs b/demo/web/tests/batch.test.mjs index b19827d72b..fb774ac7d1 100644 --- a/demo/web/tests/batch.test.mjs +++ b/demo/web/tests/batch.test.mjs @@ -2,6 +2,20 @@ import assert from "node:assert/strict"; import { test } from "node:test"; import { batchMemoryEstimate, batchOutputDirectory, memoryEstimate, MEMORY_LIMIT } from "../batch.mjs"; +test("rejects each invalid memory operand before arithmetic can coerce or cancel it", () => { + for (let index = 0; index < 4; index++) { + for (const value of [-1, -100000000, "1024", null, true, NaN, Infinity, 1.5, Number.MAX_SAFE_INTEGER + 1]) { + const operands = [1024, 70 * 1024 * 1024, 0, 2]; + operands[index] = value; + assert.throws(() => memoryEstimate(...operands), /unsupported file size/u); + } + } + const summaries = [{totalBytes: 1024, selectedBytes: 70 * 1024 * 1024, windowsSelectedBytes: 70 * 1024 * 1024, files: 2}]; + assert.ok(batchMemoryEstimate(summaries, "linux", 0) > MEMORY_LIMIT); + assert.throws(() => batchMemoryEstimate(summaries, "linux", -100000000), /unsupported file size/u); + assert.equal(memoryEstimate(0, 0, 0, 0), 8 * 1024 * 1024); +}); + test("estimates all ZIP outputs against one shared memory limit", () => { const summaries = [ { totalBytes: 1024, selectedBytes: 128, windowsSelectedBytes: 256, files: 2 },