Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pabigot opened this issue Nov 23, 2015 · 0 comments
Closed
Labels
buffer Issues and PRs related to the buffer subsystem.

Comments

@pabigot
Copy link
Contributor

pabigot commented Nov 23, 2015

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.

pabigot added a commit to pabigot/node that referenced this issue Nov 23, 2015
The algorithm used to convert negative values to hex generates incorrect
values when the low byte(s) of the value are zero because a carried
subtraction is applied prematurely.

Fixes nodejs#3992

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
@mscdex mscdex added the buffer Issues and PRs related to the buffer subsystem. label Nov 23, 2015
pabigot added a commit to pabigot/buffer-layout that referenced this issue Nov 23, 2015
The common Buffer.writeInt{L,B}E methods have a bug; if it appears to be
present in the running version of node substitute the fixed code.

See: nodejs/node#3992
pabigot added a commit to pabigot/buffer-layout that referenced this issue Nov 29, 2015
Provide the patch for Node.js issue #3992 as a standalone module.

See: nodejs/node#3992
rvagg pushed a commit that referenced this issue Dec 5, 2015
The algorithm used to convert negative values to hex generates incorrect
values when the low byte(s) of the value are zero because a carried
subtraction is applied prematurely.

Fixes: #3992
PR-URL: #3994
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Signed-off-by: Peter A. Bigot <pab@pabigot.com>
MylesBorins pushed a commit that referenced this issue Dec 15, 2015
The algorithm used to convert negative values to hex generates incorrect
values when the low byte(s) of the value are zero because a carried
subtraction is applied prematurely.

Fixes: #3992
PR-URL: #3994
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Signed-off-by: Peter A. Bigot <pab@pabigot.com>
jasnell pushed a commit that referenced this issue Dec 17, 2015
The algorithm used to convert negative values to hex generates incorrect
values when the low byte(s) of the value are zero because a carried
subtraction is applied prematurely.

Fixes: #3992
PR-URL: #3994
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Signed-off-by: Peter A. Bigot <pab@pabigot.com>
jasnell pushed a commit that referenced this issue Dec 23, 2015
The algorithm used to convert negative values to hex generates incorrect
values when the low byte(s) of the value are zero because a carried
subtraction is applied prematurely.

Fixes: #3992
PR-URL: #3994
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Signed-off-by: Peter A. Bigot <pab@pabigot.com>
scovetta pushed a commit to scovetta/node that referenced this issue Apr 2, 2016
The algorithm used to convert negative values to hex generates incorrect
values when the low byte(s) of the value are zero because a carried
subtraction is applied prematurely.

Fixes: nodejs#3992
PR-URL: nodejs#3994
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Signed-off-by: Peter A. Bigot <pab@pabigot.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem.
Projects
None yet
Development

No branches or pull requests

2 participants