Inexplicable "RangeError: Attempt to write outside buffer bounds"

  • Version: v6.2.0
  • Platform: Windows 10, possibly others
  • Subsystem: buffer

I've had multiple users report a RangeError: Attempt to write outside buffer bounds error that originates inside the Buffer constructor.

Here's some code that triggers it, according to the bug reports that I've received.

var buf = new Buffer(infoHash, 'hex')

Example stack traces:

Uncaught RangeError: Attempt to write outside buffer bounds
Buffer.write @ buffer.js:772
fromString @ buffer.js:238
Buffer.from @ buffer.js:131
Buffer @ buffer.js:112
Wire.handshake @ .../node_modules/bittorrent-protocol/index.js:192
RangeError: Attempt to write outside buffer bounds
    at Buffer.write (buffer.js:772:11)
    at fromString (buffer.js:238:26)
    at Function.Buffer.from (buffer.js:131:12)
    at new Buffer (buffer.js:112:17)
    at Function.encode.buffer (../webtorrent-cli/node_modules/bencode/lib/encode.js:61:17)

The call to buf.write that's crashing is in core, and it's here. Is the buffer pool offset incorrect somehow?

I don't understand how this could happen. Apologies for not having a reproducible test case. I haven't been able to reproduce it yet and I'm out of ideas.