From cc0b02357982bac6afea9e8335ac548b271e158e Mon Sep 17 00:00:00 2001 From: JSap0914 Date: Sun, 21 Jun 2026 21:55:31 +0900 Subject: [PATCH] fix: prevent _decimalPlaces infinite loop when multiplier overflows When x is a finite number smaller than ~1e-305, the multiplier e grows beyond Number.MAX_VALUE and overflows to Infinity. At that point: - Math.round(x * Infinity) / Infinity evaluates to NaN - NaN !== x is always true, causing an infinite loop Add an isFinite(e) guard that exits the loop and returns undefined, consistent with the function's existing behavior for non-finite inputs. Fixes: _decimalPlaces(1e-305), _decimalPlaces(Number.MIN_VALUE), etc. Related: #5992 --- src/helpers/helpers.math.ts | 3 +++ test/specs/helpers.math.tests.js | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/helpers/helpers.math.ts b/src/helpers/helpers.math.ts index 0fd2a95132c..de784e5f1a8 100644 --- a/src/helpers/helpers.math.ts +++ b/src/helpers/helpers.math.ts @@ -116,6 +116,9 @@ export function _decimalPlaces(x: number) { while (Math.round(x * e) / e !== x) { e *= 10; p++; + if (!isFinite(e)) { + return; + } } return p; } diff --git a/test/specs/helpers.math.tests.js b/test/specs/helpers.math.tests.js index 938742959da..4a7fbf3e932 100644 --- a/test/specs/helpers.math.tests.js +++ b/test/specs/helpers.math.tests.js @@ -36,6 +36,9 @@ describe('Chart.helpers.math', function() { expect(decimalPlaces(undefined)).toBe(undefined); expect(decimalPlaces(12345678.1234)).toBe(4); expect(decimalPlaces(1234567890.1234567)).toBe(7); + // Numbers so small that the multiplier overflows to Infinity — must not loop forever + expect(decimalPlaces(1e-305)).toBe(undefined); + expect(decimalPlaces(Number.MIN_VALUE)).toBe(undefined); }); it('should get an angle from a point', function() {