From cbe4bf2ba8109ede73605a9287d6afc07b655ea6 Mon Sep 17 00:00:00 2001 From: Marcio Teixeira Date: Sun, 14 Jul 2019 09:12:48 -0600 Subject: [PATCH] Fix process_injected_command undefined behavior (#14602) --- Marlin/src/gcode/queue.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Marlin/src/gcode/queue.cpp b/Marlin/src/gcode/queue.cpp index d519378a2..08cc26eb9 100644 --- a/Marlin/src/gcode/queue.cpp +++ b/Marlin/src/gcode/queue.cpp @@ -170,16 +170,19 @@ bool GCodeQueue::process_injected_command() { char c; size_t i = 0; while ((c = pgm_read_byte(&injected_commands_P[i])) && c != '\n') i++; - if (i) { - char cmd[i + 1]; - memcpy_P(cmd, injected_commands_P, i); - cmd[i] = '\0'; + // Extract current command and move pointer to next command + char cmd[i + 1]; + memcpy_P(cmd, injected_commands_P, i); + cmd[i] = '\0'; + injected_commands_P = c ? injected_commands_P + i + 1 : nullptr; + + // Execute command if non-blank + if (i) { parser.parse(cmd); PORT_REDIRECT(SERIAL_PORT); gcode.process_parsed_command(); } - injected_commands_P = c ? injected_commands_P + i + 1 : nullptr; return true; }