Skip to content

Commit a1cd9c0

Browse files
laljikanjareeyajkwlui
authored andcommittedNov 15, 2019
fix: safe integer bounds conversion (#576)
* fix: safe integer bound and added test cases * test: added more unit tests for outside of bounds
1 parent ad0d678 commit a1cd9c0

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
 

‎src/mutation.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class Mutation {
111111
// tslint:disable-next-line no-any
112112
const num = Long.fromBytes(buf as any).toNumber();
113113

114-
if (Number.MIN_SAFE_INTEGER < num && num < Number.MAX_SAFE_INTEGER) {
114+
if (Number.isSafeInteger(num)) {
115115
return num;
116116
}
117117
}

‎test/mutation.ts

+49
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,55 @@ describe('Bigtable/Mutation', function() {
5656

5757
assert.strictEqual(num, decoded);
5858
});
59+
60+
it('should convert a base64 encoded MIN_SAFE_INTEGER number when true', function() {
61+
const num = Number.MIN_SAFE_INTEGER;
62+
const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString(
63+
'base64'
64+
);
65+
const decoded = Mutation.convertFromBytes(encoded, {
66+
isPossibleNumber: true,
67+
});
68+
69+
assert.strictEqual(num, decoded);
70+
});
71+
72+
it('should convert a base64 encoded MAX_SAFE_INTEGER number when true', function() {
73+
const num = Number.MAX_SAFE_INTEGER;
74+
const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString(
75+
'base64'
76+
);
77+
const decoded = Mutation.convertFromBytes(encoded, {
78+
isPossibleNumber: true,
79+
});
80+
81+
assert.strictEqual(num, decoded);
82+
});
83+
84+
it('should not convert a base64 encoded smaller than MIN_SAFE_INTEGER number when true', function() {
85+
const num = Number.MIN_SAFE_INTEGER - 100;
86+
const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString(
87+
'base64'
88+
);
89+
const decoded = Mutation.convertFromBytes(encoded, {
90+
isPossibleNumber: true,
91+
});
92+
93+
assert.notStrictEqual(num, decoded);
94+
});
95+
96+
it('should not convert a base64 encoded larger than MAX_SAFE_INTEGER number when true', function() {
97+
const num = Number.MAX_SAFE_INTEGER + 100;
98+
const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString(
99+
'base64'
100+
);
101+
const decoded = Mutation.convertFromBytes(encoded, {
102+
isPossibleNumber: true,
103+
});
104+
105+
assert.notStrictEqual(num, decoded);
106+
});
107+
59108
it('should not convert a base64 encoded number when false', function() {
60109
const num = 10;
61110
const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString(

0 commit comments

Comments
 (0)