Skip to content
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

check unexpected high bits for invalid characters #173

Merged
merged 3 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion lib/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
function parseHex (str, start, end) {
var r = 0;
var len = Math.min(str.length, end);
var z = 0;
for (var i = start; i < len; i++) {
var c = str.charCodeAt(i) - 48;

Expand All @@ -206,9 +207,11 @@
b = c;
}

assert(c >= 0 && b <= 0xf, 'Invalid character');
r |= b;
z |= b;
}

assert(!(z & 0xf0), 'Invalid character in ' + str);
return r;
}

Expand Down
18 changes: 18 additions & 0 deletions test/constructor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ describe('BN.js/Constructor', function () {
res;
}, /Invalid character/);
});

it('should not accept non-hex characters', function () {
[
'0000000z',
'000000gg',
'0000gg00',
'fffggfff',
'/0000000',
'0-000000', // if -, is first, that is OK
'ff.fffff',
'hexadecimal'
].forEach(function (str) {
assert.throws(function () {
var res = new BN(str, 16);
res;
}, /Invalid character in /);
});
});
});

describe('with Array input', function () {
Expand Down