webtorrent / bittorrent-tracker

🌊 Simple, robust, BitTorrent tracker (client & server) implementation
https://webtorrent.io
MIT License
1.78k stars 317 forks source link

RangeError: attempt to write outside buffer bounds #94

Closed olalonde closed 8 years ago

olalonde commented 8 years ago

Getting this error when my client tries to connect to a webrtc peer:

...
  simple-peer [bf9f303] signal +4s
buffer.js:522
    throw new RangeError('attempt to write outside buffer bounds');
    ^

RangeError: attempt to write outside buffer bounds
    at Buffer.write (buffer.js:522:11)
    at fromString (buffer.js:115:26)
    at new Buffer (buffer.js:54:12)
    at Object.exports.hexToBinary (./node_modules/torrent-discovery/node_modules/bittorrent-tracker/lib/common.js:15:10)
    at Peer.<anonymous> (./node_modules/torrent-discovery/node_modules/bittorrent-tracker/lib/client/websocket-tracker.js:272:26)
    at Peer.g (events.js:260:16)
    at emitOne (events.js:77:13)
    at Peer.emit (events.js:169:7)
    at Peer.sendOffer (./node_modules/torrent-discovery/node_modules/bittorrent-tracker/node_modules/simple-peer/index.js:309:12)
    at Peer.g (events.js:260:16)
olalonde commented 8 years ago

Interestingly, I added a console.log here:

exports.hexToBinary = function (str) {
  console.log(str.length);
  return new Buffer(str, 'hex').toString('binary')
}

and apparently, the string is just 40 characters...

olalonde commented 8 years ago

Now getting a similar error here:

node_modules/torrent-discovery/node_modules/bittorrent-tracker/node_modules/ws/lib/Receiver.js:386
    default: srcBuffer.copy(dstBuffer, dstOffset, 0, length); break;
                       ^

RangeError: out of range index
    at RangeError (native)
    at fastCopy (/node_modules/torrent-discovery/node_modules/bittorrent-tracker/node_modules/ws/lib/Receiver.js:386:24)
    at Receiver.add (/node_modules/torrent-discovery/node_modules/bittorrent-tracker/node_modules/ws/lib/Receiver.js:86:3)
    at TLSSocket.realHandler (/node_modules/torrent-discovery/node_modules/bittorrent-tracker/node_modules/ws/lib/WebSocket.js:800:20)
    at emitOne (events.js:77:13)
    at TLSSocket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:146:16)
    at TLSSocket.Readable.push (_stream_readable.js:110:10)
    at TLSWrap.onread (net.js:523:20)

Strange... Maybe this is due to the wrtc module not being compiled properly?

olalonde commented 8 years ago

Well, those errors seem to go away with node 0.12.7.

kumavis commented 8 years ago

Getting a similar error from an entirely different code base - but I notice the ws and wrtc modules are in common

feross commented 8 years ago

@olalonde Since your issue was fixed by upgrading to a newer version of node, I am going to close this issue now.

@kumavis It would be interesting to know what version of node you're using.

kumavis commented 8 years ago

got the same error on v4.2.3 and v5.3.0

feross commented 8 years ago

Sadly, I can't figure out what the issue might be without more information. Are you using a memory-constrained device like a Raspberry Pi? What do you think might be causing this?

kumavis commented 8 years ago

no, modern laptop - I don't think the actual issue matches the error message. I think wrtc somehow replaced the native Buffer ? I'm not really sure, and I'm not familiar with how native bindings work. I've avoided this issue by running in headless electron and using their wrtc classes. This is not a problem for me anymore, so I'm not going to pursue it. but thank you for following up.

feross commented 8 years ago

Okay, thanks for letting me know. I think we're going to move away from wrtc in webtorrent-hybrid so hopefully this won't be an issue for anyone else in the future.

kumavis commented 8 years ago

@feross curious what you plan to replace it with. I was daydreaming about a module that would summon a headless electron process, setup an rpc over a stream, and create remote versions of the wrtc classes that mirrored their behaviour in the electron runtime. but seems like a large api surface area and would be a challenge to reproduce any synchronous behaviour.

feross commented 8 years ago

@kumavis - that's actually something I've though of before! I think it actually might be possible too :)

However, I'm currently placing my hopes in https://github.com/nickdesaulniers/node-rtc-peer-connection by @nickdesaulniers. If that works out, it'll be a pure JavaScript WebRTC implementation, and all our problems will be solved :+1:

kumavis commented 8 years ago

ah, nice! the javascript singularity approaches...

feross commented 8 years ago

Indeed!

paulkernfeld commented 8 years ago

FWIW, I got this same error in an unrelated project on Node 5.5 because I tried to send a Buffer through a node-webrtc DataChannel. I don't know how that could have caused the original error, though, because it seems like simple-peer takes care of converting from Buffers.

For anyone who searches for this error message, you might instead want to check out js-platform/node-webrtc#103, "Allow sending node.js Buffer objects."

olalonde commented 8 years ago

@paulkernfeld thanks for the link. At this point, I'm really hoping someone would take over node-webrtc and do a rewrite from scratch. If only I didn't suck at C++...

feross commented 8 years ago

There's an issue about this on the main webtorrent repo: https://github.com/feross/webtorrent/issues/820 where we've made progress toward fixing this.