Skip to content

Unreachable code in buffer.js and unsigned bitwise shifts. #2668

Closed
@ChALkeR

Description

@ChALkeR

Thing to note here: A >>> B converts both arguments to unsigned int: http://www.ecma-international.org/ecma-262/6.0/#sec-unsigned-right-shift-operator.

  1. buffer.js#L314 is not reachable. If argument start is a number that is less than 0, then start >>> 0 makes it a (large) positive number. Fixed.
  2. buffer.js#L498 has offset < 0 check that is not reachable, because above it was either set to 0 or offset >>> 0 or length >>> 0.
  3. buffer.js#L498 has length < 0 check that is reachable only through XXX legacy write(string, encoding, offset, length) - remove in v0.13 case, because else it was set to either 0, undefined, or this.length (which should be probably not less than 0 I guess). On a side note: sould that legacy path be removed in 4.0 or not? Btw, in the «legacy write(» case there is no type checking or casting for length.

Also, on many other lines, offset = offset >>> 0 and a succeeding checkInt(this, value, offset is done.

Note that -30064771000 >>> 0 === 72, which makes -30064771000 a valid offset input, which could be a bit unexpected. Also, checkInt doesn't check for offset (or offset + ext) to be not less than zero. Such check would be unreachable now (because offset is always cast to unsigned), but it could be usable once this is fixed.

var x = new Buffer(10).fill(0);
x.writeUInt32BE(0xdeadbeef, -30064771070);
x

<Buffer 00 00 de ad be ef 00 00 00 00>

Btw, with these negative values the same happens also when >> or | (or any other cast-to-int) is used: -30064771070 >> 0 === 2, -30064771070 | 0 === 0. All checks should be probably done before int casting.

Maybe it's worth splitting this into two issues — unreachable code and casting before checks.

@trevnorris

Metadata

Metadata

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