Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensureSlash: Fix accidental string-to-NaN coercion #4424

Merged

Conversation

wchargin
Copy link
Contributor

@wchargin wchargin commented May 8, 2018

Summary:
The hasSlash method uses path.substr(path, path.length - 1) to
remove the last character from path. Clearly, the first parameter is
suspect; it should be 0. The code works as written, but only very
accidentally: the first parameter is coerced by ToNumber to NaN,
which is then coerced by ToInteger to +0, per the spec.

Test Plan:
Reading the spec should be sufficient. To verify in the Real World:

const path = "has-slash-but-does-not-need-slash/"
const a = path.substr(path, path.length - 1);
const b = path.substr(0, path.length - 1);
console.log(a === b);  // true
console.log(a);        // has-slash-but-does-not-need-slash

wchargin-branch: ensureslash-accidental-coercion

Summary:
The `hasSlash` method uses `path.substr(path, path.length - 1)` to
remove the last character from `path`. Clearly, the first parameter is
suspect; it should be `0`. The code works as written, but only very
accidentally: the first parameter is coerced by `ToNumber` to `NaN`,
which is then coerced by `ToInteger` to `+0`, per [the spec][1].

[1]: https://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.substr

Test Plan:
Reading the spec should be sufficient. To verify in the Real World:
```js
const path = "has-slash-but-does-not-need-slash/"
const a = path.substr(path, path.length - 1);
const b = path.substr(0, path.length - 1);
console.log(a === b);  // true
console.log(a);        // has-slash-but-does-not-need-slash
```

wchargin-branch: ensureslash-accidental-coercion
@Timer Timer added this to the 2.0.0 milestone May 8, 2018
@Timer
Copy link
Contributor

Timer commented May 8, 2018

Wow, excellent catch and analysis. Thanks!

@Timer Timer merged commit 3aaddef into facebook:next May 8, 2018
@wchargin
Copy link
Contributor Author

wchargin commented May 8, 2018

You're welcome! Thanks for the quick response.

@wchargin
Copy link
Contributor Author

wchargin commented May 9, 2018

(Thanks to @decentralion for spotting this while adding Flow types to some of our code. :-) )

zmitry pushed a commit to zmitry/create-react-app that referenced this pull request Sep 30, 2018
Summary:
The `hasSlash` method uses `path.substr(path, path.length - 1)` to
remove the last character from `path`. Clearly, the first parameter is
suspect; it should be `0`. The code works as written, but only very
accidentally: the first parameter is coerced by `ToNumber` to `NaN`,
which is then coerced by `ToInteger` to `+0`, per [the spec][1].

[1]: https://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.substr

Test Plan:
Reading the spec should be sufficient. To verify in the Real World:
```js
const path = "has-slash-but-does-not-need-slash/"
const a = path.substr(path, path.length - 1);
const b = path.substr(0, path.length - 1);
console.log(a === b);  // true
console.log(a);        // has-slash-but-does-not-need-slash
```

wchargin-branch: ensureslash-accidental-coercion
@lock lock bot locked and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants