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

Math.min NOT work with 123021 numbers #36958

Closed
ihgazni2 opened this issue Jan 16, 2021 · 7 comments
Closed

Math.min NOT work with 123021 numbers #36958

ihgazni2 opened this issue Jan 16, 2021 · 7 comments
Labels
known limitation Issues that are identified as known limitations.

Comments

@ihgazni2
Copy link

ihgazni2 commented Jan 16, 2021

  • Version: v16.0.0-pre,v12.18.2
  • Platform: linux
  • Subsystem: ubuntu

What steps will reproduce the bug?

>

function creat_arr(lngth2,lngth3){
    let arr_two = Array.from({length:lngth2}).map(r=>2)
    let arr_three = Array.from({length:lngth3}).map(r=>3)
    let arr = Array.prototype.concat(arr_two,arr_three)
    return(arr)
}

var arr = creat_arr(63124,60166)
Math.min(...arr)


Uncaught RangeError: Maximum call stack size exceeded

/*
> arr[63123]
2
> arr[63124]
3
>

> arr.filter(r=>r===2).length
63124
> arr.filter(r=>r===3).length
60166
>


[
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2,
  ... 123190 more items
]

*/


>
> var arr = creat_arr(6312,6016)
undefined
> Math.min(...arr)
2
>
> var arr = creat_arr(63124,59896)
undefined
> Math.min(...arr)
2
> var arr = creat_arr(63124,59897)
undefined
> Math.min(...arr)
Uncaught RangeError: Maximum call stack size exceeded
    at Math.min (<anonymous>)
>

How often does it reproduce? Is there a required condition?

always, if with too many params

What is the expected behavior?

Math.min(...arr)
2

What do you see instead?

Uncaught RangeError: Maximum call stack size exceeded
at Math.min ()

Additional information

@aduh95 aduh95 added v8 engine Issues and PRs related to the V8 dependency. repl Issues and PRs related to the REPL subsystem. confirmed-bug Issues with confirmed bugs. and removed v8 engine Issues and PRs related to the V8 dependency. labels Jan 16, 2021
@aduh95
Copy link
Contributor

aduh95 commented Jan 16, 2021

It seems to happen only on REPL and when using eval (E.G.: node -p 'Math.min(...Array.from({length:1<<10<<10}, ()=>2))'). Saving the script to a file and execute it works perfectly though.

I can also reproduce using Reflect.apply(Math.min, null, arr).

@targos targos added known limitation Issues that are identified as known limitations. and removed confirmed-bug Issues with confirmed bugs. labels Jan 16, 2021
@targos
Copy link
Member

targos commented Jan 16, 2021

This is not a bug, rather a known limitation. See #16870

@aduh95
Copy link
Contributor

aduh95 commented Jan 16, 2021

This is not a bug, rather a known limitation. See #16870

I'm not sure this is exactly the same problem, as it doesn't happen not on DevTools, nor on Node.js executing a file – so it's not a V8 limitation. It seems there's another limitation with REPL and eval, which we may be able to workaround.

@targos
Copy link
Member

targos commented Jan 16, 2021

it doesn't happen not on DevTools, nor on Node.js executing a file

I don't know what you mean exactly. The example code you gave in #36958 (comment) also fails if executed from a file or in Chrome devtools

@aduh95
Copy link
Contributor

aduh95 commented Jan 16, 2021

I've run the OP code on DevTools (V8 8.7) it outputs the expected result:

image

And on Node.js (V8 8.6) it works only when executing a file rather than eval a string:

$ cat math:min.js
function creat_arr(lngth2,lngth3){
    let arr_two = Array.from({length:lngth2}).map(r=>2)
    let arr_three = Array.from({length:lngth3}).map(r=>3)
    let arr = Array.prototype.concat(arr_two,arr_three)
    return(arr)
}

var arr = creat_arr(63124,60166)
console.log(Math.min(...arr))
$ node math:min.js                                                            
2
$ node < math:min.js 
[stdin]:9
console.log(Math.min(...arr))
                 ^

RangeError: Maximum call stack size exceeded
    at [stdin]:9:18
    at Script.runInThisContext (node:vm:133:18)
    at Object.runInThisContext (node:vm:310:38)
    at node:internal/process/execution:77:19
    at [stdin]-wrapper:6:22
    at evalScript (node:internal/process/execution:76:60)
    at node:internal/main/eval_stdin:29:5
    at ReadStream.<anonymous> (node:internal/process/execution:205:5)
    at ReadStream.emit (node:events:376:20)
    at endReadableNT (node:internal/streams/readable:1295:12)

@targos
Copy link
Member

targos commented Jan 16, 2021

It's probably because depending on the situations, the code stays below your stack size. If you execute the same code with larger arrays you should be able to reproduce everywhere.

@BridgeAR BridgeAR removed the repl Issues and PRs related to the REPL subsystem. label Apr 10, 2021
@BridgeAR
Copy link
Member

I am closing this as there's nothing that can be done on Node.js side about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
known limitation Issues that are identified as known limitations.
Projects
None yet
Development

No branches or pull requests

4 participants