From 21d0b0099800f6ee9387aca8b46ccfd6281541c8 Mon Sep 17 00:00:00 2001 From: Smile Date: Fri, 21 Oct 2022 14:48:14 -0700 Subject: [PATCH 1/2] add divide by 0 protection in timer.js getLeft() --- debug_toolbar/static/debug_toolbar/js/timer.js | 8 +++++++- docs/changes.rst | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/debug_toolbar/static/debug_toolbar/js/timer.js b/debug_toolbar/static/debug_toolbar/js/timer.js index 70d3fe5a2..8a90e4f16 100644 --- a/debug_toolbar/static/debug_toolbar/js/timer.js +++ b/debug_toolbar/static/debug_toolbar/js/timer.js @@ -6,7 +6,13 @@ function insertBrowserTiming() { timingEnd = performance.timing.loadEventEnd, totalTime = timingEnd - timingOffset; function getLeft(stat) { - return ((performance.timing[stat] - timingOffset) / totalTime) * 100.0; + if (totalTime !== 0) { + return ( + ((performance.timing[stat] - timingOffset) / totalTime) * 100.0 + ); + } else { + return 0; + } } function getCSSWidth(stat, endStat) { let width = diff --git a/docs/changes.rst b/docs/changes.rst index 720a8c050..f1128de60 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -4,6 +4,8 @@ Change log Pending ------- +* Added protection against division by 0 in timer.js + 3.7.0 (2022-09-25) ------------------ From eeb0e8684fc394cff167f0756f88262cbaa07a16 Mon Sep 17 00:00:00 2001 From: Smile Date: Fri, 21 Oct 2022 15:16:08 -0700 Subject: [PATCH 2/2] add divide by 0 protection in timer.js getCSSWidth() --- .../static/debug_toolbar/js/timer.js | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/debug_toolbar/static/debug_toolbar/js/timer.js b/debug_toolbar/static/debug_toolbar/js/timer.js index 8a90e4f16..6db5ce276 100644 --- a/debug_toolbar/static/debug_toolbar/js/timer.js +++ b/debug_toolbar/static/debug_toolbar/js/timer.js @@ -15,12 +15,20 @@ function insertBrowserTiming() { } } function getCSSWidth(stat, endStat) { - let width = - ((performance.timing[endStat] - performance.timing[stat]) / - totalTime) * - 100.0; - // Calculate relative percent (same as sql panel logic) - width = (100.0 * width) / (100.0 - getLeft(stat)); + let width = 0; + if (totalTime !== 0) { + width = + ((performance.timing[endStat] - performance.timing[stat]) / + totalTime) * + 100.0; + } + const denominator = 100.0 - getLeft(stat); + if (denominator !== 0) { + // Calculate relative percent (same as sql panel logic) + width = (100.0 * width) / denominator; + } else { + width = 0; + } return width < 1 ? "2px" : width + "%"; } function addRow(tbody, stat, endStat) {