mohitsh / jslibs

Automatically exported from code.google.com/p/jslibs
0 stars 0 forks source link

readInt with unsupported size throws, but still removes bytes from the buffer #107

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Execute the following code:

loadModule('jsstd');
loadModule('jsio');

var b = new Buffer();
var p = new Pack(b);

p.writeInt(1,1);
p.writeInt(2,1);
p.writeInt(3,1);
p.writeInt(4,1);

try{
  var foo = p.readInt(3);
}
catch (e) {
  //
}

print(p.readInt(1));

The readInt() call with unsupported argument 3 fails as expected. The print
statement echoes "4", but was expected to echo "1".

Changing the print() call to:

print(p.buffer.length);

...returns 1, but was expected to return 4.

Hypothesis: 3 bytes were removed from the start of Buffer b via Pack p,
even though the readInt call failed, and even though readInt claims not to
support 3-byte reads.

Desired: Failed readInt call should not remove bytes from the buffer.

Better solution: readInt should support reading any number of bytes from 1
to a reasonable finite positive integer.

Original issue reported on code.google.com by phil_rho...@rocketmail.com on 19 Dec 2009 at 4:39

GoogleCodeExporter commented 9 years ago

Original comment by sou...@gmail.com on 19 Dec 2009 at 11:51

GoogleCodeExporter commented 9 years ago
fixed in the trunk (r3049):
If the conversion is not supported, read bytes are moved back to the buffer 
before
throwing the exception.

> Better solution: readInt should support reading any number of bytes from 1
> to a reasonable finite positive integer.
I think this is the topic for another bug.

Original comment by sou...@gmail.com on 19 Dec 2009 at 12:34

GoogleCodeExporter commented 9 years ago
Thanks!

Original comment by phil_rho...@rocketmail.com on 19 Dec 2009 at 12:50