Skip to content

Fix operator precedence in reportCurrentSpeed time-delta calculation#168

Open
claudioabreu wants to merge 1 commit into
openspeedtest:mainfrom
claudioabreu:fix-operator-precedence
Open

Fix operator precedence in reportCurrentSpeed time-delta calculation#168
claudioabreu wants to merge 1 commit into
openspeedtest:mainfrom
claudioabreu:fix-operator-precedence

Conversation

@claudioabreu

Copy link
Copy Markdown

Problem

In reportCurrentSpeed() the time-delta lines read:

dtLoad = dtDiff = 0 ? 0 : dTime - dtDiff;   // download branch
utLoad = utDiff = 0 ? 0 : Tym  - utDiff;    // upload branch

The intent is clearly “if this is the first sample, the delta is 0”, but due to operator precedence this parses as:

dtLoad = (dtDiff = (0 ? 0 : dTime - dtDiff));

The ternary condition is the constant 0 (always false), so the first-sample case is never handled — the first delta is computed as dTime - 0 instead of 0, and dtDiff is transiently assigned the wrong value before being overwritten on the next line.

Fix

Compare explicitly:

dtLoad = dtDiff === 0 ? 0 : dTime - dtDiff;
utLoad = utDiff === 0 ? 0 : Tym  - utDiff;

In app-2.5.4.min.js the 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).

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant