Skip to content

Commit

Permalink
[jsinterp] Handle NaN in bitwise operators
Browse files Browse the repository at this point in the history
Closes #6131
  • Loading branch information
pukkandan committed May 19, 2023
1 parent f7f7a87 commit 1d76561
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions test/test_jsinterp.py
Expand Up @@ -445,6 +445,16 @@ def test_bitwise_operators_overflow(self):
jsi = JSInterpreter('function x(){return 1236566549 << 5}')
self.assertEqual(jsi.call_function('x'), 915423904)

def test_bitwise_operators_typecast(self):
jsi = JSInterpreter('function x(){return null << 5}')
self.assertEqual(jsi.call_function('x'), 0)

jsi = JSInterpreter('function x(){return undefined >> 5}')
self.assertEqual(jsi.call_function('x'), 0)

jsi = JSInterpreter('function x(){return 42 << NaN}')
self.assertEqual(jsi.call_function('x'), 42)

def test_negative(self):
jsi = JSInterpreter("function f(){return 2 * -2.0;}")
self.assertEqual(jsi.call_function('f'), -4)
Expand Down
4 changes: 4 additions & 0 deletions test/test_youtube_signature.py
Expand Up @@ -146,6 +146,10 @@
'https://www.youtube.com/s/player/6f20102c/player_ias.vflset/en_US/base.js',
'lE8DhoDmKqnmJJ', 'pJTTX6XyJP2BYw',
),
(
'https://www.youtube.com/s/player/cfa9e7cb/player_ias.vflset/en_US/base.js',
'aCi3iElgd2kq0bxVbQ', 'QX1y8jGb2IbZ0w',
),
]


Expand Down
7 changes: 6 additions & 1 deletion yt_dlp/jsinterp.py
Expand Up @@ -20,7 +20,12 @@

def _js_bit_op(op):
def zeroise(x):
return 0 if x in (None, JS_Undefined) else x
if x in (None, JS_Undefined):
return 0
with contextlib.suppress(TypeError):
if math.isnan(x): # NB: NaN cannot be checked by membership
return 0
return x

def wrapped(a, b):
return op(zeroise(a), zeroise(b)) & 0xffffffff
Expand Down

0 comments on commit 1d76561

Please sign in to comment.