1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-02-23 00:56:03 +01:00

Bugfix: serial COMMAND_BUFFER_SIZE could be silently overrun

This commit is contained in:
Harald Barth 2025-01-31 11:17:59 +01:00
parent 9054d8d9f5
commit 0154e7fd78

View File

@ -126,29 +126,33 @@ void SerialManager::loop2() {
buffer[0] = '\0'; buffer[0] = '\0';
} }
} else { // if (inCommandPayload) } else { // if (inCommandPayload)
if (bufferLength < (COMMAND_BUFFER_SIZE-1)) if (bufferLength < (COMMAND_BUFFER_SIZE-1)) {
buffer[bufferLength++] = ch; buffer[bufferLength++] = ch; // advance bufferLength
if (inCommandPayload > PAYLOAD_NORMAL) { if (inCommandPayload > PAYLOAD_NORMAL) {
if (inCommandPayload > 32 + 2) { // String way too long if (inCommandPayload > 32 + 2) { // String way too long
ch = '>'; // we end this nonsense ch = '>'; // we end this nonsense
inCommandPayload = PAYLOAD_NORMAL; inCommandPayload = PAYLOAD_NORMAL;
DIAG(F("Parse error: Unbalanced string")); DIAG(F("Parse error: Unbalanced string"));
// fall through to ending parsing below // fall through to ending parsing below
} else if (ch == '"') { // String end } else if (ch == '"') { // String end
inCommandPayload = PAYLOAD_NORMAL; inCommandPayload = PAYLOAD_NORMAL;
continue; // do not fall through continue; // do not fall through
} else } else
inCommandPayload++; inCommandPayload++;
} }
if (inCommandPayload == PAYLOAD_NORMAL) { if (inCommandPayload == PAYLOAD_NORMAL) {
if (ch == '>') { if (ch == '>') {
buffer[bufferLength] = '\0'; buffer[bufferLength] = '\0'; // This \0 is after the '>'
DCCEXParser::parse(serial, buffer, NULL); DCCEXParser::parse(serial, buffer, NULL); // buffer parsed with trailing '>'
inCommandPayload = PAYLOAD_FALSE; inCommandPayload = PAYLOAD_FALSE;
break; break;
} else if (ch == '"') { } else if (ch == '"') {
inCommandPayload = PAYLOAD_STRING; inCommandPayload = PAYLOAD_STRING;
} }
}
} else {
DIAG(F("Parse error: input buffer overflow"));
inCommandPayload = PAYLOAD_FALSE;
} }
} }
} }