Skip to content

Changes parseLengthCodedNumber to parse big numbers #382

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

Merged
merged 18 commits into from
Jan 31, 2013
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions lib/protocol/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,23 @@ Parser.prototype.parseLengthCodedNumber = function() {
throw new Error('parseLengthCodedNumber: Unexpected first byte: ' + byte);
}

var value = 0;
var value = 0, bigint = false, BigNumber = null;
for (var bytesRead = 0; bytesRead < length; bytesRead++) {
var byte = this._buffer[this._offset++];
value += Math.pow(256, bytesRead) * byte;
}

if (value >= IEEE_754_BINARY_64_PRECISION) {
throw new Error(
'parseLengthCodedNumber: JS precision range exceeded, ' +
'number is >= 53 bit: "' + value + '"'
);
// overflow
if (BigNumber) {
value = value.plus((new BigNumber(256)).pow(bytesRead).times(byte));
} else if (bytesRead == 7 && byte == 1) {
BigNumber = require("bignumber.js");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will make blocking FS calls, even if the module has been cached before. If you want to lazy-load this, you'll need to have a top-level variable that you use as an in-module cache.

edit: never mind, I see you're doing that. ignore me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, @felixge you were right. That variable is within the Parser.prototype.parseLengthCodedNumber function and should probably be moved out to the module level, otherwise require will be called each time a length coded number is parsed that is too large.

value = new BigNumber(value);
value = value.plus((new BigNumber(256)).pow(bytesRead).times(byte));
} else {
value += Math.pow(256, bytesRead) * byte;
}
}

return value;
return (BigNumber ? value.toString() : value);
};

Parser.prototype.parseFiller = function(length) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"node": "*"
},
"dependencies": {
"require-all": "0.0.3"
"require-all": "0.0.3",
"bignumber.js": "1.0.1"
},
"devDependencies": {
"utest": "0.0.6",
Expand Down