-
Notifications
You must be signed in to change notification settings - Fork 152
Closed
Description
Just testing the new v3 of ByteBuffer and noticed a bug when calculating the varint32 encoded byte length of the length of bytes of the string.
Currently, the value of 'k' is used to calculate 'l' before 'k' is set to a value. This means the value of 'i' is always 1 and therefore the offset calculation is not correct for longer strings and can throw an overflow exception if the buffer is not resized properly.
Just need to switch the two lines around to fix the problem:
// Existing:
ByteBuffer.prototype.writeVString = function(str, offset) {
...
var start = offset,
k, l;
l = ByteBuffer.calculateVarint32(k);
k = utf8_calc_string(str);
offset += l+k;
...
};
// Revised:
ByteBuffer.prototype.writeVString = function(str, offset) {
...
var start = offset,
k, l;
k = utf8_calc_string(str);
l = ByteBuffer.calculateVarint32(k);
offset += l+k;
...
};