-
Notifications
You must be signed in to change notification settings - Fork 23
Fix overflow when reading negative s8 #16
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -214,15 +214,9 @@ KaitaiStream.prototype.readS4be = function(e) { | |||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||
KaitaiStream.prototype.readS8be = function(e) { | ||||||||||||||||||||||||||||||||||||||||||||||||
this.ensureBytesLeft(8); | ||||||||||||||||||||||||||||||||||||||||||||||||
var v1 = this.readU4be(); | ||||||||||||||||||||||||||||||||||||||||||||||||
var v2 = this.readU4be(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
if ((v1 & 0x80000000) != 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
// negative number | ||||||||||||||||||||||||||||||||||||||||||||||||
return -(0x100000000 * (v1 ^ 0xffffffff) + (v2 ^ 0xffffffff)) - 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||
return 0x100000000 * v1 + v2; | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
var high = this.readU4be(); | ||||||||||||||||||||||||||||||||||||||||||||||||
var low = this.readU4be(); | ||||||||||||||||||||||||||||||||||||||||||||||||
return KaitaiStream.twoU4sToS8(high, low); | ||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
// ........................................................................ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -260,15 +254,9 @@ KaitaiStream.prototype.readS4le = function(e) { | |||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||
KaitaiStream.prototype.readS8le = function(e) { | ||||||||||||||||||||||||||||||||||||||||||||||||
this.ensureBytesLeft(8); | ||||||||||||||||||||||||||||||||||||||||||||||||
var v1 = this.readU4le(); | ||||||||||||||||||||||||||||||||||||||||||||||||
var v2 = this.readU4le(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
if ((v2 & 0x80000000) != 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
// negative number | ||||||||||||||||||||||||||||||||||||||||||||||||
return -(0x100000000 * (v2 ^ 0xffffffff) + (v1 ^ 0xffffffff)) - 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||
return 0x100000000 * v2 + v1; | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
var low = this.readU4le(); | ||||||||||||||||||||||||||||||||||||||||||||||||
var high = this.readU4le(); | ||||||||||||||||||||||||||||||||||||||||||||||||
return KaitaiStream.twoU4sToS8(high, low); | ||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
// ------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -802,6 +790,18 @@ KaitaiStream.createStringFromArray = function(array) { | |||||||||||||||||||||||||||||||||||||||||||||||
return chunks.join(""); | ||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
KaitaiStream.twoU4sToS8 = function(high, low) { | ||||||||||||||||||||||||||||||||||||||||||||||||
if ((high & 0x80000000) != 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
// negative number | ||||||||||||||||||||||||||||||||||||||||||||||||
high = high ^ 0xffffffff; | ||||||||||||||||||||||||||||||||||||||||||||||||
low = low ^ 0xffffffff; | ||||||||||||||||||||||||||||||||||||||||||||||||
low = low < 0 ? 2**32 + low : low; | ||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't use exponentiation operator in JavaScript, because this would noticeably impair the KS runtime compatibility with JavaScript environments (compare compatibility table on MDN of exponentiation
Moreover, environments that don't know about exponential operator I believe that in theory you can bring the current KS runtime up and running even in ancient browsers like IE 5 🙂, if you link a few polyfills. I suggest a cleaner (and probably also faster) way how to convert the 32-bit signed integer to an unsigned one - using the unsigned right shift operator. From MDN:
So it's enough to do
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
return -(0x100000000 * high + low) - 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||
return 0x100000000 * high + low; | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
return KaitaiStream; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always use strict equality operator
===
(or!==
negated) in JavaScript unless you have a very good reason to use loose equality op with type juggling (==
/!=
).The loose equality op
==
knows several ways how to surprise you: at random"" == false
,"" == 0
,[] == false
(whereas![] === false
actually) ,[] == ''
,[] == 0
,[0] == false
,[1] == true
,[null, null] == ","
etc. Thus it's rarely a good idea to use the loose equality.It doesn't matter whether you think you know the type of the operands, because it's JavaScript. Unless you are doing
typeof X === ...
everywhere, you don't know the types.