From 28b62e06bd426b53edef6d49b1c868601705e8d6 Mon Sep 17 00:00:00 2001 From: bigmouseears Date: Fri, 13 Feb 2026 11:21:39 -0700 Subject: [PATCH 1/2] Added check to prevent receiving messages larger than a specific buffer size --- lora/lora.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lora/lora.c b/lora/lora.c index 307a3b6..006cd6b 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -400,9 +400,15 @@ LORA_STATUS lora_receive_ready() { // ============================================================================= // lora_receive: receive a buffer from lora fifo with continuous mode // ============================================================================= -LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ +LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr, uint8_t* max_buffer_len){ uint8_t timeout_flag; + + if (*buffer_len_ptr > *max_buffer_len) { + return LORA_FAIL; + } + + // LORA_STATUS rx_done = lora_receive_ready(); // We check if we've received a packet. // If we haven't received a packet, the output will be similar to lora_receive_ready in that case. From 6f12c77d0d1be2f1dce567041e7c7d63e3d21047 Mon Sep 17 00:00:00 2001 From: bigmouseears Date: Tue, 24 Feb 2026 18:08:14 -0700 Subject: [PATCH 2/2] Added specific error type for buffer overflows. --- lora/lora.c | 14 +++++++------- lora/lora.h | 3 ++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lora/lora.c b/lora/lora.c index 006cd6b..f9a8193 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -400,13 +400,8 @@ LORA_STATUS lora_receive_ready() { // ============================================================================= // lora_receive: receive a buffer from lora fifo with continuous mode // ============================================================================= -LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr, uint8_t* max_buffer_len){ +LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t buffer_len, uint8_t* num_bytes_received) { uint8_t timeout_flag; - - - if (*buffer_len_ptr > *max_buffer_len) { - return LORA_FAIL; - } // LORA_STATUS rx_done = lora_receive_ready(); // We check if we've received a packet. @@ -429,6 +424,11 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr, uint8_t* uint8_t num_bytes; LORA_STATUS fifo2_status = lora_read_register(LORA_REG_FIFO_RX_NUM_BYTES, &num_bytes); + if (num_bytes > buffer_len) + { + return LORA_BUFFER_UNDERSIZED; + } + #if defined( TESTRECEIVER ) char buf[255]; int len = sprintf( buf, "Numbytes: %d\r\n", num_bytes ); @@ -456,7 +456,7 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr, uint8_t* pld_xtr_status = lora_read_register(LORA_REG_FIFO_RW, &packet); // Access LoRA FIFO data buffer pointer buffer_ptr[i] = packet; } - *buffer_len_ptr = num_bytes; + *num_bytes_received = num_bytes; if (pld_xtr_status == LORA_OK ) { return LORA_OK; } else { diff --git a/lora/lora.h b/lora/lora.h index cdabf3e..2e81f14 100644 --- a/lora/lora.h +++ b/lora/lora.h @@ -39,6 +39,7 @@ typedef enum LORA_STATUS { LORA_TRANSMIT_FAIL, LORA_RECEIVE_FAIL, LORA_TIMEOUT_FAIL, + LORA_BUFFER_UNDERSIZED, LORA_READY, LORA_WAITING } LORA_STATUS; @@ -165,6 +166,6 @@ LORA_STATUS lora_transmit(uint8_t* buffer_ptr, uint8_t buffer_len); LORA_STATUS lora_receive_ready(); -LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr); +LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t buffer_len, uint8_t* num_bytes_received); #endif \ No newline at end of file