From 842613e27d4fa993191ab60f3fb133217b96f884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20Gr=C3=B6n?= Date: Fri, 3 Feb 2023 14:16:11 +0000 Subject: [PATCH] Fix array out of bounds access bug This bug is triggered if a line ends with a command without a number. --- src/parser.cpp | 3 +++ test/parser_tests.cpp | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/src/parser.cpp b/src/parser.cpp index c78f9e5..4f459a6 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -247,6 +247,9 @@ namespace gpr { string cs = parse_line_comment_with_delimiter(";", s); return chunk(';', ';', cs); } else { + if (s.i + 1 >= s.s.size()) { + return parse_isolated_word(s); + } string next_next = *(s.remaining() + 1); if (!is_num_char(next_next[0])) { diff --git a/test/parser_tests.cpp b/test/parser_tests.cpp index b988c5a..6f962d5 100644 --- a/test/parser_tests.cpp +++ b/test/parser_tests.cpp @@ -151,6 +151,12 @@ namespace gpr { REQUIRE(p.get_block(0).get_chunk(4).tp() == CHUNK_TYPE_WORD); } + TEST_CASE("Parsing block only an isolated word 'P'") { + gcode_program p = parse_gcode("P"); + + REQUIRE(p.get_block(0).get_chunk(0).tp() == CHUNK_TYPE_WORD); + } + TEST_CASE("Parse blank line") { gcode_program p = parse_gcode("G99 G82 R0.1 Z-0.1227 P F15.04\n ");