From e924509e561f7ada230117b63cbe827c980c2d70 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Wed, 29 Nov 2017 15:47:27 -0500 Subject: [PATCH] lib: better validation of string input Fix: #151 --- lib/bn.js | 13 +++++++++---- test/constructor-test.js | 11 +++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/bn.js b/lib/bn.js index 251d761..0c41a1b 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -191,18 +191,23 @@ r <<= 4; + var b; + // 'a' - 'f' if (c >= 49 && c <= 54) { - r |= c - 49 + 0xa; + b = c - 49 + 0xa; // 'A' - 'F' } else if (c >= 17 && c <= 22) { - r |= c - 17 + 0xa; + b = c - 17 + 0xa; // '0' - '9' } else { - r |= c & 0xf; + b = c; } + + assert(c >= 0 && b <= 0xf, 'Invalid character'); + r |= b; } return r; } @@ -258,7 +263,7 @@ } else { b = c; } - assert(b < mul, 'Invalid character'); + assert(c >= 0 && b < mul, 'Invalid character'); r += b; } return r; diff --git a/test/constructor-test.js b/test/constructor-test.js index caae696..9f27203 100644 --- a/test/constructor-test.js +++ b/test/constructor-test.js @@ -99,11 +99,14 @@ describe('BN.js/Constructor', function () { it('should not accept decimal', function () { assert.throws(function () { - BN('10.00', 10); - }); + var res = new BN('10.00', 10); + res; + }, /Invalid character/); + assert.throws(function () { - BN('16.00', 16); - }); + var res = new BN('16.00', 16); + res; + }, /Invalid character/); }); });