From ce9dee9370173c76784100bd2b7c08ced76143d2 Mon Sep 17 00:00:00 2001 From: Tom James Holub Date: Sat, 22 Jul 2017 09:26:30 -0700 Subject: [PATCH 1/3] util.readNumber: stop overflowing until full range of uint32 | #497 --- src/util.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/util.js b/src/util.js index 78394ad4..f2d8bdef 100644 --- a/src/util.js +++ b/src/util.js @@ -85,12 +85,9 @@ export default { readNumber: function (bytes) { var n = 0; - for (var i = 0; i < bytes.length; i++) { - n <<= 8; - n += bytes[i]; + n += Math.pow(256, i) * bytes[bytes.length - 1 - i]; } - return n; }, From bcf4a114a7cfde4f6f7d8d582a79e9f8a96044a6 Mon Sep 17 00:00:00 2001 From: Tom James Holub Date: Sat, 22 Jul 2017 09:27:36 -0700 Subject: [PATCH 2/3] util.readNumber: overflow test added | #497 --- test/general/util.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/general/util.js b/test/general/util.js index d805eef1..985cd5a6 100644 --- a/test/general/util.js +++ b/test/general/util.js @@ -185,6 +185,12 @@ describe('Util unit tests', function() { var test = openpgp.util.decode_utf8.bind(null, {chameleon: true}); expect(test).to.throw(Error, /Parameter "utf8" is not of type string/); }); + it('util.readNumber should not overflow untill full range of uint32', function () { + var ints = [Math.pow(2, 20), Math.pow(2, 25), Math.pow(2, 30), Math.pow(2, 32) - 1]; + for(var i = 0; i < ints.length; i++) { + expect(openpgp.util.readNumber(openpgp.util.writeNumber(ints[i], 4))).to.equal(ints[i]); + } + }); }); }); From 265fa626011eb1447ff30ecc9cb056fb61491963 Mon Sep 17 00:00:00 2001 From: Bart Butler Date: Sat, 22 Jul 2017 09:38:44 -0700 Subject: [PATCH 3/3] Correct misspelling --- test/general/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/general/util.js b/test/general/util.js index 985cd5a6..06d75bb6 100644 --- a/test/general/util.js +++ b/test/general/util.js @@ -185,7 +185,7 @@ describe('Util unit tests', function() { var test = openpgp.util.decode_utf8.bind(null, {chameleon: true}); expect(test).to.throw(Error, /Parameter "utf8" is not of type string/); }); - it('util.readNumber should not overflow untill full range of uint32', function () { + it('util.readNumber should not overflow until full range of uint32', function () { var ints = [Math.pow(2, 20), Math.pow(2, 25), Math.pow(2, 30), Math.pow(2, 32) - 1]; for(var i = 0; i < ints.length; i++) { expect(openpgp.util.readNumber(openpgp.util.writeNumber(ints[i], 4))).to.equal(ints[i]);