Skip to content

Commit

Permalink
bn: don't accept invalid characters
Browse files Browse the repository at this point in the history
Contructor from string accepted characters out of base range.
Fix by throwing exception when such character appears in the string.
  • Loading branch information
dchest authored and indutny committed Nov 29, 2017
1 parent b267676 commit 9827640
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@

function parseBase (str, start, end, mul) {
var r = 0;
var b = 0;
var len = Math.min(str.length, end);
for (var i = start; i < len; i++) {
var c = str.charCodeAt(i) - 48;
Expand All @@ -247,16 +248,18 @@

// 'a'
if (c >= 49) {
r += c - 49 + 0xa;
b = c - 49 + 0xa;

// 'A'
} else if (c >= 17) {
r += c - 17 + 0xa;
b = c - 17 + 0xa;

// '0' - '9'
} else {
r += c;
b = c;
}
assert(b < mul, 'Invalid character');
r += b;
}
return r;
}
Expand Down
6 changes: 6 additions & 0 deletions test/constructor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ describe('BN.js/Constructor', function () {
assert.equal(new BN('1A6B765D8CDF', 16, 'le').toString(16),
'df8c5d766b1a');
});

it('should not accept wrong characters for base', function () {
assert.throws(function () {
return new BN('01FF');
}, /^Error: Invalid character$/);
});
});

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

0 comments on commit 9827640

Please sign in to comment.