From 01919b33df5b7a2854e3cbae8508de8e23ef8ef9 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Wed, 30 Aug 2023 23:55:39 +0200 Subject: [PATCH] Make parser more fool proof --- DCCEXParser.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 49d2d20..53cb5ce 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -451,12 +451,16 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) #ifndef DISABLE_PROG case 'w': // WRITE CV on MAIN - DCC::writeCVByteMain(p[0], p[1], p[2]); - return; + if (params != 3) + break; + DCC::writeCVByteMain(p[0], p[1], p[2]); + return; case 'b': // WRITE CV BIT ON MAIN - DCC::writeCVBitMain(p[0], p[1], p[2], p[3]); - return; + if (params != 4) + break; + DCC::writeCVBitMain(p[0], p[1], p[2], p[3]); + return; #endif case 'M': // WRITE TRANSPARENT DCC PACKET MAIN @@ -479,14 +483,16 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) #ifndef DISABLE_PROG case 'W': // WRITE CV ON PROG - if (!stashCallback(stream, p, ringStream)) - break; + if (!stashCallback(stream, p, ringStream)) + break; if (params == 1) // Write new loco id (clearing consist and managing short/long) DCC::setLocoId(p[0],callback_Wloco); else if (params == 4) // WRITE CV ON PROG DCC::writeCVByte(p[0], p[1], callback_W4); - else // WRITE CV ON PROG + else if (params == 2) // WRITE CV ON PROG DCC::writeCVByte(p[0], p[1], callback_W); + else + break; return; case 'V': // VERIFY CV ON PROG @@ -506,9 +512,11 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) } break; - case 'B': // WRITE CV BIT ON PROG + case 'B': // WRITE CV BIT ON PROG or + if (params != 3 && params != 5) + break; if (!stashCallback(stream, p, ringStream)) - break; + break; DCC::writeCVBit(p[0], p[1], p[2], callback_B); return; @@ -642,7 +650,7 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream) case 'D': // < > if (parseD(stream, params, p)) return; - return; + break; #endif case '=': // <= Track manager control > if (TrackManager::parseJ(stream, params, p))