Merge pull request #566 from FlowCrypt/master

util.readNumber: stop overflowing until full range of uint32 for 4 byte numbers | #497
This commit is contained in:
Bart Butler 2017-07-22 09:58:00 -07:00 committed by GitHub
commit dc2d38d355
2 changed files with 7 additions and 4 deletions

View File

@ -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;
},

View File

@ -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 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]);
}
});
});
});