From e306421f471090011e483b502232a078250c10ce Mon Sep 17 00:00:00 2001 From: Carter Turn Date: Sun, 28 Apr 2024 16:12:54 -0400 Subject: [PATCH] Add null terminator to strings returned from fast_serial_read_until --- prawn_do/fast_serial.c | 3 ++- prawn_do/fast_serial.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/prawn_do/fast_serial.c b/prawn_do/fast_serial.c index af27da4..add1531 100644 --- a/prawn_do/fast_serial.c +++ b/prawn_do/fast_serial.c @@ -35,7 +35,7 @@ uint32_t fast_serial_read(const char * buffer, uint32_t buffer_size){ // Read bytes until terminator reached (blocks until terminator or buffer_size is reached) uint32_t fast_serial_read_until(char * buffer, uint32_t buffer_size, char until){ uint32_t buffer_idx = 0; - while(buffer_idx < buffer_size){ + while(buffer_idx < buffer_size - 1){ while(fast_serial_read_available() > 0){ int32_t next_char = tud_cdc_read_char(); @@ -51,6 +51,7 @@ uint32_t fast_serial_read_until(char * buffer, uint32_t buffer_size, char until) } fast_serial_task(); } + buffer[buffer_idx] = '\0'; // Null terminate string return buffer_idx; } diff --git a/prawn_do/fast_serial.h b/prawn_do/fast_serial.h index 0e0b3e5..f77cda9 100644 --- a/prawn_do/fast_serial.h +++ b/prawn_do/fast_serial.h @@ -46,6 +46,7 @@ static inline uint32_t fast_serial_read_atomic(const char * buffer, uint32_t buf uint32_t fast_serial_read(const char * buffer, uint32_t buffer_size); // Read bytes until terminator reached (blocks until terminator or buffer_size is reached) +// Adds null terminator to buffer after read completes (reserving one byte in buffer for this) uint32_t fast_serial_read_until(char * buffer, uint32_t buffer_size, char until); // Clear read FIFO (without reading it)