Skip to content

Buffer.writeInt{L,B}E generates wrong bytes for some negative integers #3992

Closed
@pabigot

Description

@pabigot

The common Buffer implementation for writing integer values uses a rather opaque algorithm that involves carrying a -1 adjustment into high bytes when converting negative integers. The adjustment is improperly applied when lower bytes are zero.

An example:

> var buf = new Buffer(4);
undefined
> buf.writeInt32LE(-0x10000, 0);
4
> buf.toJSON().data
[ 0, 0, 255, 255 ]
> buf.writeIntLE(-0x10000, 0, 4);
4
> buf.toJSON().data
[ 0, 255, 254, 255 ]

This bug is present in v0.12.x through HEAD.

Patch incoming.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions