From c65785280b5cc140eed9885f9f5a2b44d3f36631 Mon Sep 17 00:00:00 2001 From: itxaiohanglover <1531137510@qq.com> Date: Sun, 21 Jun 2026 03:01:18 +0800 Subject: [PATCH] fix: guard against empty BCD encoding scheme to prevent TypeError Validate the encoding scheme argument in To BCD and From BCD before it is used. When the scheme is empty or unrecognised (e.g. loaded from a recipe URL), ENCODING_LOOKUP returns undefined, and accessing properties on it threw an uncaught TypeError. Throw a friendly OperationError instead. Adds regression tests covering both operations with an empty scheme. --- src/core/operations/FromBCD.mjs | 3 +++ src/core/operations/ToBCD.mjs | 3 +++ tests/operations/tests/BCD.mjs | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/core/operations/FromBCD.mjs b/src/core/operations/FromBCD.mjs index 8fa990a4..5be1b911 100644 --- a/src/core/operations/FromBCD.mjs +++ b/src/core/operations/FromBCD.mjs @@ -73,6 +73,9 @@ class FromBCD extends Operation { let output = "", byteArray; + if (encoding === undefined) + throw new OperationError("Invalid encoding scheme"); + // Normalise the input switch (inputFormat) { case "Nibbles": diff --git a/src/core/operations/ToBCD.mjs b/src/core/operations/ToBCD.mjs index 3908742c..4e880400 100644 --- a/src/core/operations/ToBCD.mjs +++ b/src/core/operations/ToBCD.mjs @@ -67,6 +67,9 @@ class ToBCD extends Operation { signed = args[2], outputFormat = args[3]; + if (encoding === undefined) + throw new OperationError("Invalid encoding scheme"); + // Split input number up into separate digits const digits = input.toFixed().split(""); diff --git a/tests/operations/tests/BCD.mjs b/tests/operations/tests/BCD.mjs index c6715e56..53d46948 100644 --- a/tests/operations/tests/BCD.mjs +++ b/tests/operations/tests/BCD.mjs @@ -100,4 +100,26 @@ TestRegister.addTests([ } ] }, + { + name: "To BCD: invalid (empty) encoding scheme", + input: "43", + expectedOutput: "Invalid encoding scheme", + recipeConfig: [ + { + "op": "To BCD", + "args": ["", true, false, "Nibbles"] + } + ] + }, + { + name: "From BCD: invalid (empty) encoding scheme", + input: "0100 0011", + expectedOutput: "Invalid encoding scheme", + recipeConfig: [ + { + "op": "From BCD", + "args": ["", true, false, "Nibbles"] + } + ] + }, ]);