Files
rea/src/evm/EvmBytecodeCarrier.test.ts
T
Dragolone 48e7470041 fix(evm): trim hex carrier whitespace in linear time (#5200)
The hex carrier trimmed outer ASCII whitespace with a global
start|end-anchored regex. For an invalid carrier with a long interior
whitespace run, the end branch was retried at every position of the run,
so rejection took quadratic time (about 18 s for a 200 KB run) and a few
hundred KB could exhaust the worker deadline instead of returning the
documented invalid-format error. Trim with index scans over the same
ASCII whitespace set.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-10-22 03:17:50 +08:00

43 lines
1.4 KiB
TypeScript

import { expect, it } from "vitest";
import {
decodeEvmBytecodeCarrier,
EvmCarrierFailure,
} from "./EvmBytecodeCarrier.js";
it.each(["6000FF", "0x6000ff", " \t0X6000FF\r\n"])(
"decodes explicit hex without changing selected bytes: %j",
(text) => {
expect(
Buffer.from(decodeEvmBytecodeCarrier(Buffer.from(text), "hex")).toString(
"hex",
),
).toBe("6000ff");
},
);
it.each(["0x1", "60 00", "0xzz", "\uFEFF6000", "\u00a06000"])(
"rejects malformed carrier rather than guessing bytecode: %j",
(text) => {
expect(() => decodeEvmBytecodeCarrier(Buffer.from(text), "hex")).toThrow(
EvmCarrierFailure,
);
},
);
it("rejects a long interior separator run in linear time", () => {
const text = `60${" ".repeat(200_000)}00`;
const started = performance.now();
expect(() => decodeEvmBytecodeCarrier(Buffer.from(text), "hex")).toThrow(
EvmCarrierFailure,
);
expect(performance.now() - started).toBeLessThan(1_000);
});
it("distinguishes raw bytes, empty code and malformed UTF-8", () => {
const bytes = Uint8Array.from([0xff, 0x00]);
const decoded = decodeEvmBytecodeCarrier(bytes, "raw");
expect(decoded).toEqual(bytes);
expect(decoded).not.toBe(bytes);
expect(() => decodeEvmBytecodeCarrier(bytes, "hex")).toThrow("valid UTF-8");
expect(decodeEvmBytecodeCarrier(Buffer.from("0x"), "hex")).toEqual(
new Uint8Array(),
);
});