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

Different outcome cli vs file input #12052

Closed
pvdz opened this issue Mar 26, 2017 · 1 comment
Closed

Different outcome cli vs file input #12052

pvdz opened this issue Mar 26, 2017 · 1 comment

Comments

@pvdz
Copy link

pvdz commented Mar 26, 2017

  • Version:
    v6.10.1-pre
    (although this appears to affect multiple versions)

  • Platform:
    Linux fox 4.4.0-64-generic Update README.md #85-Ubuntu SMP Mon Feb 20 11:50:30 UTC 2017 x86_64 x86_64 x86_64

Test case:

var a = 0;
var i = 5;
do {
  try {
  } finally {
    var NaN = --a;
  }
} while (NaN && --i > 0)
console.log(a);

Test output:

$ node -v
v6.10.1-pre
$ uname -a
Linux fox 4.4.0-64-generic #85-Ubuntu SMP Mon Feb 20 11:50:30 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
$ cat s.js
var a = 0;
var i = 5;
do {
  try {
  } finally {
    var NaN = --a;
  }
} while (NaN && --i > 0)
console.log(a);

$ node s.js
-5
$ cat s.js | node
-1
$ 

From mishoo/UglifyJS#1695. I was fuzzing the minifier when I ran into this case. The output when ran from file is different as when ran when piping cli.

The code doesn't seem to do anything in particular that would cause this difference, although redefining NaN could be its own reason.

@bnoordhuis
Copy link
Member

It's not a bug so much as it's an artifact of the module system. node s.js wraps the script in a function wrapper where var NaN is a local variable; node < s.js runs it in the global context where it overwrites the global NaN.

If you add a 'use strict' or run node --use_strict < s.js, you can see the difference:

$ out/Release/node --use_strict < tmp/bug12052.js
[stdin]:6
    var NaN = --a;
                ^

TypeError: Cannot assign to read only property 'NaN' of object '#<Object>'
    at [stdin]:6:17
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at Object.runInThisContext (vm.js:116:38)
    [...]

Hope that helps.

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

No branches or pull requests

2 participants