From 7faa0c48d7b73ebe2c8c78f36b9cd3591b23be85 Mon Sep 17 00:00:00 2001 From: LatoWolf Date: Sun, 7 Sep 2025 18:30:57 +0800 Subject: [PATCH] Update KBinJSON.ts Fix when pug introduced an empty element --- src/utils/KBinJSON.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/utils/KBinJSON.ts b/src/utils/KBinJSON.ts index 1a28073..89158ac 100644 --- a/src/utils/KBinJSON.ts +++ b/src/utils/KBinJSON.ts @@ -505,6 +505,16 @@ function nodeToBinary( binaryType = 'u8'; } + // Normalize missing or malformed content so kencode doesn't crash. + if (value === null || value === undefined) { + // If node is flagged as array (via __count), default to an empty array. + // Otherwise default to a single zero value for numeric types. + value = isArray ? [] : [0]; + } else if (!Array.isArray(value)) { + // Ensure numeric/binary values are arrays for the writer APIs. + value = [value as number | bigint]; + } + if (isArray) { dataBuf.writeArray(binaryType as BinaryLengthType, value as (number | bigint)[]); } else {