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.
This commit is contained in:
itxaiohanglover 2026-06-21 03:01:18 +08:00
parent a0a369a7ef
commit c65785280b
3 changed files with 28 additions and 0 deletions

View File

@ -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":

View File

@ -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("");

View File

@ -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"]
}
]
},
]);