blickly / closure-compiler-issues

0 stars 0 forks source link

2<<32 causes problems #77

Closed blickly closed 9 years ago

blickly commented 9 years ago
mike@haven:~$ echo &quot;document.write(2&lt;&lt;32)&quot;|java -jar compiler.jar
stdin:1: ERROR - Shift amount out of bounds: NUMBER 32.0 1
document.write(2&lt;&lt;32)
                  ^

1 error(s), 0 warning(s)
mike@haven:~$

Is this a bug in closure? I came across it when attempting to compress the
code from http://crypto.stanford.edu/sjcl/ - The YUI compressor handles it
fine...

Original issue reported on code.google.com by grepular on 2009-12-17 15:27:02

blickly commented 9 years ago
I suspect this is a bug in the cryptography code.  Closure Compiler is flagging code
that's very likely to be 
incorrect.

Although most numbers in JavaScript are represented as floating point numbers, bitwise
operators are 
represented as 32 bit integers.  The shift operators only work on integers, so 2<<32
is going to shift the bits 
off the end of the integer.  If you try this out in Firefox, you'll find that alert(2<<32)
returns 2, alert(2<<31) 
returns 0, and alert(2<<30) returns -2147483648.  I don't think that's what they intended.

Considering the offending code is checking if the length of the array to be encrypted
is greater than 2^16 but 
less than 2^32, I suspect no one's noticed this because they've never encrypted strings
longer than 65K.  I'm 
also surprised they're not doing 1<<32 to get 2^32 rather than 2^33, but don't know
if this was intentional.

Original issue reported on code.google.com by bowdidge@google.com on 2009-12-17 17:28:26

blickly commented 9 years ago
Marking as behaves correctly/will not fix.  If you think this really is a bug, let us
know why in the bug and we'll 
see what we can do.

Original issue reported on code.google.com by bowdidge@google.com on 2009-12-18 17:08:52