From fd1be923b2fdedf4f5ed5ded418591c32181ed11 Mon Sep 17 00:00:00 2001 From: atsiv sivat Date: Sun, 8 Mar 2026 00:59:21 +0000 Subject: [PATCH] add function to enforce base 10 decimal sytnax before parsing --- src/core/lib/IEEEBinary.mjs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/core/lib/IEEEBinary.mjs b/src/core/lib/IEEEBinary.mjs index fc407245b..cf805c7f3 100644 --- a/src/core/lib/IEEEBinary.mjs +++ b/src/core/lib/IEEEBinary.mjs @@ -36,6 +36,12 @@ function safeBigInt(str, context="number") { } } +function validateDecimal(input) { + // validates optional-sign base-10 decimal input: integers, decimals with optional digits before or after the decimal point, and scientific notation + if (!/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i.test(input)) + throw new Error("Invalid decimal number"); +} + /** * Compute 10^exp as BigInt without using ** on BigInt, to avoid @@ -212,6 +218,8 @@ export function ToIEEE754Float64(input) { return `${s} ${expOnes} ${mantZeros}`; } + validateDecimal(input); + let sign = 0n; if (input.startsWith("-")) { sign = 1n;