Skip to content

Commit 3aaddef

Browse files
wcharginTimer
authored andcommitted
ensureSlash: Fix accidental string-to-NaN coercion (#4424)
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
1 parent e5e9f59 commit 3aaddef

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

packages/react-scripts/config/paths.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const envPublicUrl = process.env.PUBLIC_URL;
2323
function ensureSlash(path, needsSlash) {
2424
const hasSlash = path.endsWith('/');
2525
if (hasSlash && !needsSlash) {
26-
return path.substr(path, path.length - 1);
26+
return path.substr(0, path.length - 1);
2727
} else if (!hasSlash && needsSlash) {
2828
return `${path}/`;
2929
} else {

0 commit comments

Comments
 (0)