Skip to content

Commit 62b326e

Browse files
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.
1 parent a0a369a commit 62b326e

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/core/operations/FromBCD.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class FromBCD extends Operation {
7373
let output = "",
7474
byteArray;
7575

76+
if (encoding === undefined)
77+
throw new OperationError("Invalid encoding scheme");
78+
7679
// Normalise the input
7780
switch (inputFormat) {
7881
case "Nibbles":

src/core/operations/ToBCD.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class ToBCD extends Operation {
6767
signed = args[2],
6868
outputFormat = args[3];
6969

70+
if (encoding === undefined)
71+
throw new OperationError("Invalid encoding scheme");
72+
7073
// Split input number up into separate digits
7174
const digits = input.toFixed().split("");
7275

tests/operations/tests/BCD.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,28 @@ TestRegister.addTests([
100100
}
101101
]
102102
},
103+
{
104+
name: "To BCD: empty encoding scheme throws error",
105+
input: "43",
106+
expectedError: true,
107+
expectedOutput: "To BCD - Invalid encoding scheme",
108+
recipeConfig: [
109+
{
110+
"op": "To BCD",
111+
"args": ["", true, false, "Nibbles"]
112+
}
113+
]
114+
},
115+
{
116+
name: "From BCD: empty encoding scheme throws error",
117+
input: "0100 0011",
118+
expectedError: true,
119+
expectedOutput: "From BCD - Invalid encoding scheme",
120+
recipeConfig: [
121+
{
122+
"op": "From BCD",
123+
"args": ["", true, false, "Nibbles"]
124+
}
125+
]
126+
},
103127
]);

0 commit comments

Comments
 (0)