fix hexdump width validation

This commit is contained in:
skywalker 2026-06-06 02:27:37 +08:00 committed by sky
parent d735496641
commit b50bb88104
2 changed files with 18 additions and 1 deletions

View File

@ -8,6 +8,8 @@ import Operation from "../Operation.mjs";
import Utils from "../Utils.mjs"; import Utils from "../Utils.mjs";
import OperationError from "../errors/OperationError.mjs"; import OperationError from "../errors/OperationError.mjs";
const MAX_WIDTH = 65536;
/** /**
* To Hexdump operation * To Hexdump operation
*/ */
@ -30,7 +32,8 @@ class ToHexdump extends Operation {
"name": "Width", "name": "Width",
"type": "number", "type": "number",
"value": 16, "value": 16,
"min": 1 "min": 1,
"max": MAX_WIDTH
}, },
{ {
"name": "Upper case hex", "name": "Upper case hex",
@ -63,6 +66,9 @@ class ToHexdump extends Operation {
if (length < 1 || Math.round(length) !== length) if (length < 1 || Math.round(length) !== length)
throw new OperationError("Width must be a positive integer"); throw new OperationError("Width must be a positive integer");
if (length > MAX_WIDTH)
throw new OperationError(`Width must be no more than ${MAX_WIDTH}`);
const lines = []; const lines = [];
for (let i = 0; i < data.length; i += length) { for (let i = 0; i < data.length; i += length) {
let lineNo = Utils.hex(i, 8); let lineNo = Utils.hex(i, 8);

View File

@ -126,6 +126,17 @@ TestRegister.addTests([
} }
], ],
}, },
{
name: "To Hexdump: Width too large",
input: "H",
expectedOutput: "Width must be no more than 65536",
recipeConfig: [
{
op: "To Hexdump",
args: [155555555555555, false, false, false]
}
],
},
{ {
name: "From Hexdump: xxd", name: "From Hexdump: xxd",
input: `00000000: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f ................ input: `00000000: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f ................