diff --git a/src/core/operations/BitShiftLeft.mjs b/src/core/operations/BitShiftLeft.mjs index cd9f4568fa..bf42cb9316 100644 --- a/src/core/operations/BitShiftLeft.mjs +++ b/src/core/operations/BitShiftLeft.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; /** * Bit shift left operation @@ -27,7 +28,8 @@ class BitShiftLeft extends Operation { { "name": "Amount", "type": "number", - "value": 1 + "value": 1, + "min": 0 } ]; } @@ -39,6 +41,9 @@ class BitShiftLeft extends Operation { */ run(input, args) { const amount = args[0]; + if (amount < 0) { + throw new OperationError("Amount must be non-negative."); + } input = new Uint8Array(input); return input.map(b => { diff --git a/tests/operations/tests/BitwiseOp.mjs b/tests/operations/tests/BitwiseOp.mjs index 50303677dd..97d889ed94 100644 --- a/tests/operations/tests/BitwiseOp.mjs +++ b/tests/operations/tests/BitwiseOp.mjs @@ -21,6 +21,15 @@ TestRegister.addTests([ "args": ["Space"] } ] }, + { + name: "Bit shift left rejects negative amount", + input: "a", + expectedOutput: "Amount must be non-negative.", + recipeConfig: [ + { "op": "Bit shift left", + "args": [-1] } + ] + }, { name: "Bit shift right: Logical shift", input: "01010101 10101010 11111111 00000000 11110000 00001111 00110011 11001100",