PSON icon indicating copy to clipboard operation
PSON copied to clipboard

Overflow when encoding a big integer

Open tdzl2003 opened this issue 8 years ago • 4 comments

When I encode a big integer(bigger than 2^31), it overflow and got a negative value in Node.js

This is because parseInt doesn't limit integer value in 32bit:

> parseInt(12345678901234)
12345678901234

Change the line:

var intVal = parseInt(val);

into:

var intVal = val | 0;

will solve this problem.

tdzl2003 avatar Sep 20 '17 14:09 tdzl2003

I can confirm that. That's a very unfortunate bug because it makes impossible to serialize timestamps thus making this package unusable for many programmers.

Simple reproducing case:

const PSON = require('pson')
const pson = new PSON.Pair()

const testData = {
	ts: Date.now()
}

const buff = pson.encode(testData)
const testDecode = pson.decode(buff)

console.log(testData.ts, testDecode.ts)
// outputs 1531344982502 -1958342170

yura415 avatar Jul 11 '18 21:07 yura415

@nonphp You can try my incoming pr #10

tdzl2003 avatar Jul 12 '18 09:07 tdzl2003

Still not working

const PSON = require('pson') // 2.0.0
const pson = new PSON.ProgressivePair(['ts'])

const testData = { ts: 15313449825 }
const buff = pson.encode(testData)
const testDecode = pson.decode(buff)

console.log(testData.ts === testDecode.ts, testData.ts, testDecode.ts)
// outputs false, 15313449825 -1866419359

Josema avatar Jan 19 '20 11:01 Josema

@Josema You can try my fork or modify that line manually and use patch-package to keep the changed line., this pr is still not merged.

tdzl2003 avatar Feb 15 '20 22:02 tdzl2003