Skip to content

ByteBuffer.writeVString offset calculation incorrect. #20

@cawilson

Description

@cawilson

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;
    ...
};

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions