Merge pull request #319 from bloqaudio/pr/cmd-editor-overflow

cmd_editor: fix cmd_buffer overflow and CLI hang on long input
This commit is contained in:
René van Dorst
2026-08-12 09:26:17 +02:00
committed by GitHub
+18 -15
View File
@@ -40,21 +40,24 @@ void cmd_edit(void) __banked
{ {
while (l != sbuf_ptr) { while (l != sbuf_ptr) {
if (sbuf[l] >= ' ' && sbuf[l] < 127) { // A printable character, copy to command line if (sbuf[l] >= ' ' && sbuf[l] < 127) { // A printable character, copy to command line
if (cmd_line_len >= CMD_BUF_SIZE) // Reserve one byte for the terminating NUL written on Enter. When the
continue; // line is full, drop the character but still fall through to advance the
write_char(sbuf[l]); // serial-ring read pointer below; a 'continue' here would spin forever.
// Shift buffer to right if (cmd_line_len < CMD_BUF_SIZE - 1) {
for (uint8_t i = cmd_line_len; i > cursor; i--) write_char(sbuf[l]);
cmd_buffer[i] = cmd_buffer[i-1]; // Shift buffer to right
// Insert char in comand buffer for (uint8_t i = cmd_line_len; i > cursor; i--)
cmd_buffer[cursor++] = sbuf[l]; cmd_buffer[i] = cmd_buffer[i-1];
cmd_line_len++; // Insert char in comand buffer
// Print rest of line cmd_buffer[cursor++] = sbuf[l];
for (uint8_t i = cursor; i < cmd_line_len; i++) cmd_line_len++;
write_char(cmd_buffer[i]); // Print rest of line
// Move backwards for (uint8_t i = cursor; i < cmd_line_len; i++)
for (uint8_t i = cursor; i < cmd_line_len; i++) write_char(cmd_buffer[i]);
write_char('\010'); // BS works like cursor-left // Move backwards
for (uint8_t i = cursor; i < cmd_line_len; i++)
write_char('\010'); // BS works like cursor-left
}
} else if (sbuf[l] == '\033') { // ESC-Sequence } else if (sbuf[l] == '\033') { // ESC-Sequence
// Wait until we have at least 3 characters including the ESC character in the serial buffer // Wait until we have at least 3 characters including the ESC character in the serial buffer
if (((sbuf_ptr + SBUF_SIZE - l) & SBUF_MASK) < 3) if (((sbuf_ptr + SBUF_SIZE - l) & SBUF_MASK) < 3)