Skip to content

Commit 0fe1ce3

Browse files
committed
Fix big number accuracy
1 parent 1aec4fd commit 0fe1ce3

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

lib/packets/packet.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,9 @@ class Packet {
222222
return word1 * 0x100000000 + word0;
223223
}
224224
res = new Long(word0, word1, !signed); // Long need unsigned
225-
const resNumber = res.toNumber();
226225
const resString = res.toString();
227-
res = resNumber.toString() === resString ? resNumber : resString;
228-
return bigNumberStrings ? resString : res;
226+
const safeResNumberPreferred = res.greaterThan(Number.MAX_SAFE_INTEGER) || res.lessThan(Number.MIN_SAFE_INTEGER) ? resString : res.toNumber();
227+
return bigNumberStrings ? resString : safeResNumberPreferred;
229228
}
230229
// eslint-disable-next-line no-console
231230
console.trace();
@@ -425,7 +424,7 @@ class Packet {
425424
return StringParser.decode(
426425
this.buffer,
427426
encoding,
428-
this.offset - len,
427+
this.offset - len,
429428
this.offset
430429
);
431430
}
@@ -450,21 +449,18 @@ class Packet {
450449
this.offset++;
451450
sign = -1;
452451
}
453-
// max precise int is 9007199254740992
452+
// max precise int is Number.MAX_SAFE_INTEGER 9007199254740991
454453
let str;
455454
const numDigits = end - this.offset;
456455
if (supportBigNumbers) {
457456
if (numDigits >= 15) {
458457
str = this.readString(end - this.offset, 'binary');
459-
result = parseInt(str, 10);
460-
if (result.toString() === str) {
461-
return sign * result;
462-
}
463-
return sign === -1 ? `-${str}` : str;
464-
}
465-
if (numDigits > 16) {
466-
str = this.readString(end - this.offset);
467-
return sign === -1 ? `-${str}` : str;
458+
str = sign === -1 ? `-${str}` : str;
459+
result = Long.fromString(str, false, 10);
460+
if (result.greaterThan(Number.MAX_SAFE_INTEGER) || result.lessThan(Number.MIN_SAFE_INTEGER)) {
461+
return result.toString();
462+
}
463+
return result.toNumber();
468464
}
469465
}
470466
if (this.buffer[this.offset] === plus) {

test/integration/connection/test-insert-bigint.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ connection.query("INSERT INTO bigs SET title='test1'", (err, result) => {
2626
connection.query("INSERT INTO bigs SET title='test2'", (err, result) => {
2727
assert.strictEqual(result.insertId, 123456790);
2828
// big int
29-
connection.query("INSERT INTO bigs SET title='test', id=9007199254740992");
29+
connection.query("INSERT INTO bigs SET title='test', id=9007199254740991");
3030
connection.query("INSERT INTO bigs SET title='test3'", (err, result) => {
3131
assert.strictEqual(
32-
Long.fromString('9007199254740993').compare(result.insertId),
32+
Long.fromString('9007199254740992').compare(result.insertId),
3333
0
3434
);
3535
connection.query(
36-
"INSERT INTO bigs SET title='test', id=90071992547409924"
36+
"INSERT INTO bigs SET title='test', id=90071992547409923"
3737
);
3838
connection.query("INSERT INTO bigs SET title='test4'", (err, result) => {
3939
assert.strictEqual(
40-
Long.fromString('90071992547409925').compare(result.insertId),
40+
Long.fromString('90071992547409924').compare(result.insertId),
4141
0
4242
);
4343
connection.query(
@@ -51,10 +51,10 @@ connection.query("INSERT INTO bigs SET title='test1'", (err, result) => {
5151
assert.strictEqual(result[1].id, 124);
5252
assert.strictEqual(result[2].id, 123456789);
5353
assert.strictEqual(result[3].id, 123456790);
54-
assert.strictEqual(result[4].id, 9007199254740992);
55-
assert.strictEqual(result[5].id, '9007199254740993');
56-
assert.strictEqual(result[6].id, '90071992547409924');
57-
assert.strictEqual(result[7].id, '90071992547409925');
54+
assert.strictEqual(result[4].id, 9007199254740991);
55+
assert.strictEqual(result[5].id, '9007199254740992');
56+
assert.strictEqual(result[6].id, '90071992547409923');
57+
assert.strictEqual(result[7].id, '90071992547409924');
5858
connection.end();
5959
}
6060
);

0 commit comments

Comments
 (0)