fix possible crash caused by malformed rate request

This commit is contained in:
2026-07-06 18:16:03 +00:00
parent 60ade723a4
commit f3b279e636
+8 -3
View File
@@ -118,9 +118,14 @@ if (schemes.interrupt.enabled) {
const parsed = message.toString().split(" "); const parsed = message.toString().split(" ");
const cmd = parsed[0]; const cmd = parsed[0];
// command is rate and the value is valid // command is rate and the value is valid
if (cmd === "rate" && parsed[1] >= schemes.interrupt["min-rate"] && parsed[1] <= schemes.interrupt["max-rate"]) { if (cmd === "rate" && parsed.length === 2 && parsed[1] >= schemes.interrupt["min-rate"] && parsed[1] <= schemes.interrupt["max-rate"]) {
// get requested rate in ms // get requested rate in ms
const rate = Number(parsed[1]) * 1000; const rate = Number(parsed[1]) * 1000;
if (isNaN(rate)) {
socket.send("error: rate <rate> must be a number");
socket.terminate();
return;
}
// if timer has not started, start it with requested rate // if timer has not started, start it with requested rate
if (!timer) { if (!timer) {
timer = setTimeout(handleInterruptSync, rate); timer = setTimeout(handleInterruptSync, rate);
@@ -142,12 +147,12 @@ if (schemes.interrupt.enabled) {
} }
// command is rate but the requested value is out of bounds, terminate socket // command is rate but the requested value is out of bounds, terminate socket
else if (cmd === "rate") { else if (cmd === "rate") {
socket.send(`error: rate must be in range [${schemes.interrupt["min-rate"]}, ${schemes.interrupt["max-rate"]}].`); socket.send(`error: rate <rate> must be in range [${schemes.interrupt["min-rate"]}, ${schemes.interrupt["max-rate"]}].`);
socket.terminate(); socket.terminate();
} }
// otherwise, command is invalid, terminate socket // otherwise, command is invalid, terminate socket
else { else {
socket.send(`error: ${cmd} command not found.`); socket.send(`error: ${cmd} command not supported.`);
socket.terminate(); socket.terminate();
} }
}); });