Bug 229843

Summary: Math.hypot checks for infinite values prematurely
Product: WebKit Reporter: Richard Gibson <richard.gibson>
Component: JavaScriptCoreAssignee: Yusuke Suzuki <ysuzuki>
Status: RESOLVED FIXED    
Severity: Normal CC: ews-watchlist, fpizlo, keith_miller, mark.lam, msaboff, ross.kirsling, saam, tzagallo, webkit-bug-importer, ysuzuki
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch ross.kirsling: review+

Description Richard Gibson 2021-09-02 16:58:43 PDT
The algorithm for Math.hypot at https://tc39.es/ecma262/#sec-math.hypot requires coercing each argument to a Number before doing anything else. However, this coercion is incorrectly aborted after encountering an infinite value.

The following expression should throw an exception, but does not:
  Math.hypot({valueOf(){ return Infinity }}, {valueOf(){ throw new Error("arguments[1]") }})
Comment 1 Yusuke Suzuki 2021-09-06 02:01:41 PDT
Created attachment 437397 [details]
Patch
Comment 2 Ross Kirsling 2021-09-06 11:51:35 PDT
Comment on attachment 437397 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=437397&action=review

r=me with comments.

> Source/JavaScriptCore/ChangeLog:8
> +        We should throw an error about non finite argument after coercing all arguments to doubles.

Don't forget the spec link. :)
https://tc39.es/ecma262/#sec-math.hypot

> JSTests/stress/math-hypot-evaluation-ordering.js:18
> +shouldThrow(() => {
> +    Math.hypot({valueOf(){ return Infinity }}, {valueOf(){ throw new Error("arguments[1]") }})
> +}, `Error: arguments[1]`);

I think this test should be upstreamed to test262.
Comment 3 Yusuke Suzuki 2021-09-07 06:50:54 PDT
Comment on attachment 437397 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=437397&action=review

>> Source/JavaScriptCore/ChangeLog:8
>> +        We should throw an error about non finite argument after coercing all arguments to doubles.
> 
> Don't forget the spec link. :)
> https://tc39.es/ecma262/#sec-math.hypot

Added.

>> JSTests/stress/math-hypot-evaluation-ordering.js:18
>> +}, `Error: arguments[1]`);
> 
> I think this test should be upstreamed to test262.

@Gibson,

do you have a plan adding that?
Comment 4 Yusuke Suzuki 2021-09-07 06:52:13 PDT
Committed r282081 (241381@main): <https://commits.webkit.org/241381@main>
Comment 5 Radar WebKit Bug Importer 2021-09-07 06:53:19 PDT
<rdar://problem/82819521>
Comment 6 Richard Gibson 2021-09-07 13:23:35 PDT
Yes, I will submit a test262 PR unless someone beats me to it.
Comment 7 Richard Gibson 2021-09-10 21:37:07 PDT
Forgot that I'd already done it: https://github.com/tc39/test262/commit/50f3fca7a0eac6b6e8e5e9aee7af3c2a05831261