Skip to content

Commit

Permalink
fix(encode): remove the unsafe integer check (#15)
Browse files Browse the repository at this point in the history
That check caused 'Integer is unsafe' errors when integers above (2^53 - 1) were encoded.

By the JSON specification, a Number is not limited in size, and that behaviour differed from the
other implementations like msgpack-lite.
  • Loading branch information
darrachequesne authored Aug 23, 2017
1 parent 682aa46 commit bb8140c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
6 changes: 0 additions & 6 deletions browser/encode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1;

function utf8Write(view, offset, str) {
var c = 0;
for (var i = 0, l = str.length; i < l; i++) {
Expand Down Expand Up @@ -91,10 +89,6 @@ function _encode(bytes, defers, value) {
return 9;
}

if (Math.abs(value) > MAX_SAFE_INTEGER) {
throw new Error('Integer is unsafe');
}

if (value >= 0) {
// positive fixnum
if (value < 0x80) {
Expand Down
5 changes: 0 additions & 5 deletions lib/encode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1;
var MICRO_OPT_LEN = 32;

// Faster for short strings than buffer.write
Expand Down Expand Up @@ -98,10 +97,6 @@ function _encode(bytes, defers, value) {
return 9;
}

if (Math.abs(value) > MAX_SAFE_INTEGER) {
throw new Error('Integer is unsafe');
}

if (value >= 0) {
// positive fixnum
if (value < 0x80) {
Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ describe('notepack', function () {
it('uint 64', function () {
check(4294967296, 'cf0000000100000000');
check(Math.pow(2, 53) - 1, 'cf001fffffffffffff');
// unsafe unsigned integer
check(Math.pow(2, 63), 'cf8000000000000000');
check(Math.pow(2, 63) + 1024, 'cf8000000000000000');
});

// NOTE: We'll always encode a positive number as a uint, but we should be
Expand Down Expand Up @@ -197,6 +200,9 @@ describe('notepack', function () {
check(-7840340234323423, 'd3ffe42540896a3a21');
// Minimum safe signed integer
check(-Math.pow(2, 53) + 1, 'd3ffe0000000000001');
// unsafe signed integer
check(-Math.pow(2, 63), 'd38000000000000000');
check(-Math.pow(2, 63) - 1024, 'd38000000000000000');
});

it('fixext 1 / undefined', function () {
Expand Down

0 comments on commit bb8140c

Please sign in to comment.