Fix operator precedence in reportCurrentSpeed time-delta calculation#168
Open
claudioabreu wants to merge 1 commit into
Open
Fix operator precedence in reportCurrentSpeed time-delta calculation#168claudioabreu wants to merge 1 commit into
claudioabreu wants to merge 1 commit into
Conversation
The intent of these lines is clearly "if this is the first sample, the
time delta is 0":
dtLoad = dtDiff = 0 ? 0 : dTime - dtDiff;
but due to operator precedence it parses as
dtLoad = (dtDiff = (0 ? 0 : dTime - dtDiff));
i.e. the ternary condition is the constant 0 (always false), so the
first-sample case is never handled and dtDiff is transiently assigned
the wrong value before being overwritten on the next line. The same
pattern exists in the upload branch (utLoad/utDiff).
Fixed to compare explicitly (dtDiff === 0 / utDiff === 0). In the
minified file the original minifier had constant-folded the dead ternary
away, so the correct comparison is reintroduced there as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In
reportCurrentSpeed()the time-delta lines read:The intent is clearly “if this is the first sample, the delta is 0”, but due to operator precedence this parses as:
The ternary condition is the constant
0(always false), so the first-sample case is never handled — the first delta is computed asdTime - 0instead of0, anddtDiffis transiently assigned the wrong value before being overwritten on the next line.Fix
Compare explicitly:
In
app-2.5.4.min.jsthe original minifier had constant-folded the dead ternary away (Xa=Ea=l-Ea), so the correct comparison is reintroduced there as well.Related to #167 — this wrong first delta is one of the sources of the bogus first speed sample.
Found while maintaining our fork (vialink/vialink-speedtest).