diff options
author | David Crocker <dcrocker@eschertech.com> | 2021-02-15 16:55:01 +0300 |
---|---|---|
committer | David Crocker <dcrocker@eschertech.com> | 2021-02-15 16:55:01 +0300 |
commit | 20e8ff3e4f9e3f1a5ac53ba0e045dffc3723382d (patch) | |
tree | 12e46e2f5412f2bc82d4c86ed62fa01f0fd7faae /src/GCodes/GCodeBuffer | |
parent | a6c1e6f32ed60ae51a1abf2a66abf47343ba4ce2 (diff) |
If a number is expected in a command but not found, report an error
Diffstat (limited to 'src/GCodes/GCodeBuffer')
-rw-r--r-- | src/GCodes/GCodeBuffer/StringParser.cpp | 16 | ||||
-rw-r--r-- | src/GCodes/GCodeBuffer/StringParser.h | 1 |
2 files changed, 14 insertions, 3 deletions
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); |