user234
user234

Reputation: 109

Node.js crashing when client sends invalid packet

I'm making a multiplayer game with node.js but it crashes when client sends invalid packet.

Server:

msg.readFloatLE(1, true);

Client:

msg.setUint8(1, 1);

So, if server is waiting for a float but client sends an int, it crashes with this error:

C:\Program Files\nodejs\node.exe: src\node_buffer.cc:752: Assertion `(offset + sizeof(T)) <= (ts_obj_length)' failed.

I'm using ws library but i'm pretty sure that it's about node.js. How can i fix this?

Upvotes: 0

Views: 252

Answers (3)

snovakovic
snovakovic

Reputation: 314

If you take a look at the node documentation for readFloatLE it clearly says it throws exception for your example.

// Throws an exception: RangeError: Index out of range
console.log(buf.readFloatLE(1));

You can take a look at documentation here

The question is what exactly do you try to accomplish? and is readFloatLE right tool for that?

Upvotes: 0

Lazyexpert
Lazyexpert

Reputation: 3154

As @Ved pointed in his answer - try/catch would be the best approach.

Just to suggest something not standard:

const defaultCriticalValue = 0.0;
const floatNum = parseFloat('a') || defaultCriticalValue;
msg.readFloatLE(floatNum, true);

Upvotes: 0

Ved
Ved

Reputation: 12093

You can handle exception using try catch

try {
  msg.readFloatLE(1, true);
}
catch(e) {
  console.log(e,"error)
}

Upvotes: 1

Related Questions