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:
commit
dc2d38d355
|
@ -85,12 +85,9 @@ export default {
|
||||||
|
|
||||||
readNumber: function (bytes) {
|
readNumber: function (bytes) {
|
||||||
var n = 0;
|
var n = 0;
|
||||||
|
|
||||||
for (var i = 0; i < bytes.length; i++) {
|
for (var i = 0; i < bytes.length; i++) {
|
||||||
n <<= 8;
|
n += Math.pow(256, i) * bytes[bytes.length - 1 - i];
|
||||||
n += bytes[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return n;
|
return n;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -185,6 +185,12 @@ describe('Util unit tests', function() {
|
||||||
var test = openpgp.util.decode_utf8.bind(null, {chameleon: true});
|
var test = openpgp.util.decode_utf8.bind(null, {chameleon: true});
|
||||||
expect(test).to.throw(Error, /Parameter "utf8" is not of type string/);
|
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]);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user