From 20e8ff3e4f9e3f1a5ac53ba0e045dffc3723382d Mon Sep 17 00:00:00 2001 From: David Crocker Date: Mon, 15 Feb 2021 13:55:01 +0000 Subject: If a number is expected in a command but not found, report an error --- src/GCodes/GCodeBuffer/StringParser.cpp | 16 +++++++++++++--- src/GCodes/GCodeBuffer/StringParser.h | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'src/GCodes/GCodeBuffer') diff --git a/src/GCodes/GCodeBuffer/StringParser.cpp b/src/GCodes/GCodeBuffer/StringParser.cpp index dfc3c18f..0fa3f4b9 100644 --- a/src/GCodes/GCodeBuffer/StringParser.cpp +++ b/src/GCodes/GCodeBuffer/StringParser.cpp @@ -1606,6 +1606,16 @@ bool StringParser::FileEnded() noexcept #endif +// Check that a number was found. If it was, advance readPointer past it. Otherwise throw an exception. +void StringParser::CheckNumberFound(const char *endptr) THROWS(GCodeException) +{ + if (endptr == gb.buffer + readPointer) + { + throw ConstructParseException("expected number after '%c'", (uint32_t)gb.buffer[readPointer - 1]); + } + readPointer = endptr - gb.buffer; +} + // Functions to read values from lines of GCode, allowing for expressions and variable substitution float StringParser::ReadFloatValue() THROWS(GCodeException) { @@ -1619,7 +1629,7 @@ float StringParser::ReadFloatValue() THROWS(GCodeException) const char *endptr; const float rslt = SafeStrtof(gb.buffer + readPointer, &endptr); - readPointer = endptr - gb.buffer; + CheckNumberFound(endptr); return rslt; } @@ -1636,7 +1646,7 @@ uint32_t StringParser::ReadUIValue() THROWS(GCodeException) // Allow "0xNNNN" or "xNNNN" where NNNN are hex digits. We could stop supporting this because we already support {0xNNNN}. const char *endptr; const uint32_t rslt = StrToU32(gb.buffer + readPointer, &endptr); - readPointer = endptr - gb.buffer; + CheckNumberFound(endptr); return rslt; } @@ -1652,7 +1662,7 @@ int32_t StringParser::ReadIValue() THROWS(GCodeException) const char *endptr; const int32_t rslt = StrToI32(gb.buffer + readPointer, &endptr); - readPointer = endptr - gb.buffer; + CheckNumberFound(endptr); return rslt; } diff --git a/src/GCodes/GCodeBuffer/StringParser.h b/src/GCodes/GCodeBuffer/StringParser.h index f7a0a347..ab274068 100644 --- a/src/GCodes/GCodeBuffer/StringParser.h +++ b/src/GCodes/GCodeBuffer/StringParser.h @@ -103,6 +103,7 @@ private: int32_t ReadIValue() THROWS(GCodeException); DriverId ReadDriverIdValue() THROWS(GCodeException); void CheckArrayLength(size_t actualLength, size_t maxLength) THROWS(GCodeException); + void CheckNumberFound(const char *endptr) THROWS(GCodeException); void CheckForMixedSpacesAndTabs() noexcept; bool ProcessConditionalGCode(const StringRef& reply, BlockType skippedBlockType, bool doingFile) THROWS(GCodeException); -- cgit v1.2.3