1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-22 23:56:13 +01:00

Bugfix: Catch stange input to parser

This commit is contained in:
Harald Barth 2023-11-16 00:27:23 +01:00
parent 1af5132e6a
commit 7bd2ba9b41
2 changed files with 26 additions and 14 deletions

View File

@ -210,8 +210,10 @@ int16_t DCCEXParser::splitValues(int16_t result[MAX_COMMAND_PARAMS], const byte
case 1: // skipping spaces before a param case 1: // skipping spaces before a param
if (hot == ' ') if (hot == ' ')
break; break;
if (hot == '\0' || hot == '>') if (hot == '\0')
return parameterCount; return -1;
if (hot == '>')
return parameterCount;
state = 2; state = 2;
continue; continue;
@ -304,14 +306,19 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
#ifndef DISABLE_EEPROM #ifndef DISABLE_EEPROM
(void)EEPROM; // tell compiler not to warn this is unused (void)EEPROM; // tell compiler not to warn this is unused
#endif #endif
byte params = 0;
if (Diag::CMD) if (Diag::CMD)
DIAG(F("PARSING:%s"), com); DIAG(F("PARSING:%s"), com);
int16_t p[MAX_COMMAND_PARAMS]; int16_t p[MAX_COMMAND_PARAMS];
while (com[0] == '<' || com[0] == ' ') while (com[0] == '<' || com[0] == ' ')
com++; // strip off any number of < or spaces com++; // strip off any number of < or spaces
byte opcode = com[0]; byte opcode = com[0];
byte params = splitValues(p, com, opcode=='M' || opcode=='P'); int16_t splitnum = splitValues(p, com, opcode=='M' || opcode=='P');
if (splitnum < 0 || splitnum >= MAX_COMMAND_PARAMS) // if arguments are broken, leave but via printing <X>
goto out;
// Because of check above we are now inside byte size
params = splitnum;
if (filterCallback) if (filterCallback)
filterCallback(stream, opcode, params, p); filterCallback(stream, opcode, params, p);
if (filterRMFTCallback && opcode!='\0') if (filterRMFTCallback && opcode!='\0')
@ -833,14 +840,18 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
break; // Will <X> if not intercepted by EXRAIL break; // Will <X> if not intercepted by EXRAIL
default: //anything else will diagnose and drop out to <X> default: //anything else will diagnose and drop out to <X>
if (opcode >= ' ' && opcode <= '~') {
DIAG(F("Opcode=%c params=%d"), opcode, params); DIAG(F("Opcode=%c params=%d"), opcode, params);
for (int i = 0; i < params; i++) for (int i = 0; i < params; i++)
DIAG(F("p[%d]=%d (0x%x)"), i, p[i], p[i]); DIAG(F("p[%d]=%d (0x%x)"), i, p[i], p[i]);
break; } else {
DIAG(F("Unprintable %x"), opcode);
}
break;
} // end of opcode switch } // end of opcode switch
// Any fallout here sends an <X> out:// Any fallout here sends an <X>
StringFormatter::send(stream, F("<X>\n")); StringFormatter::send(stream, F("<X>\n"));
} }

View File

@ -111,14 +111,15 @@ void SerialManager::loop2() {
bufferLength = 0; bufferLength = 0;
buffer[0] = '\0'; buffer[0] = '\0';
} }
else if (ch == '>') { else if (inCommandPayload) {
buffer[bufferLength] = '\0'; if (bufferLength < (COMMAND_BUFFER_SIZE-1))
DCCEXParser::parse(serial, buffer, NULL); buffer[bufferLength++] = ch;
inCommandPayload = false; if (ch == '>') {
break; buffer[bufferLength] = '\0';
} DCCEXParser::parse(serial, buffer, NULL);
else if (inCommandPayload) { inCommandPayload = false;
if (bufferLength < (COMMAND_BUFFER_SIZE-1)) buffer[bufferLength++] = ch; break;
}
} }
} }