Skip to content

Commit a859b7c

Browse files
Add divide by zero protection in timer.js (#1687)
* add divide by 0 protection in timer.js getLeft() * add divide by 0 protection in timer.js getCSSWidth() Co-authored-by: Tim Schilling <schillingt@better-simple.com>
1 parent c5226e7 commit a859b7c

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

debug_toolbar/static/debug_toolbar/js/timer.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,29 @@ function insertBrowserTiming() {
55
timingEnd = performance.timing.loadEventEnd,
66
totalTime = timingEnd - timingOffset;
77
function getLeft(stat) {
8-
return ((performance.timing[stat] - timingOffset) / totalTime) * 100.0;
8+
if (totalTime !== 0) {
9+
return (
10+
((performance.timing[stat] - timingOffset) / totalTime) * 100.0
11+
);
12+
} else {
13+
return 0;
14+
}
915
}
1016
function getCSSWidth(stat, endStat) {
11-
let width =
12-
((performance.timing[endStat] - performance.timing[stat]) /
13-
totalTime) *
14-
100.0;
15-
// Calculate relative percent (same as sql panel logic)
16-
width = (100.0 * width) / (100.0 - getLeft(stat));
17+
let width = 0;
18+
if (totalTime !== 0) {
19+
width =
20+
((performance.timing[endStat] - performance.timing[stat]) /
21+
totalTime) *
22+
100.0;
23+
}
24+
const denominator = 100.0 - getLeft(stat);
25+
if (denominator !== 0) {
26+
// Calculate relative percent (same as sql panel logic)
27+
width = (100.0 * width) / denominator;
28+
} else {
29+
width = 0;
30+
}
1731
return width < 1 ? "2px" : width + "%";
1832
}
1933
function addRow(tbody, stat, endStat) {

docs/changes.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ Change log
44
Pending
55
-------
66

7+
* Added protection against division by 0 in timer.js
78
* Auto-update History panel for JavaScript ``fetch`` requests.
89
* Support `HTMX boosting <https://htmx.org/docs/#boosting/>`__ and
910
re-rendering the toolbar after the DOM has been replaced. This reworks
1011
the JavaScript integration to put most event handlers on document.body.
1112
This means we'll have slightly slower performance, but it's easier
1213
to handle re-rendering the toolbar when the DOM has been replaced.
1314

14-
1515
3.7.0 (2022-09-25)
1616
------------------
1717

0 commit comments

Comments
 (0)