From e1131871ef9a3d7d98ba65cf6daab3e5160c116e Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Thu, 19 Jan 2023 23:18:28 +0100 Subject: [PATCH] ringbuffer: alloc no less than 2 bytes. This protects against race conditions. We can now preset the first two bytes which are used for size and flags by the COM module (currently the only user of the ringbuffer module). --- avr/ringbuffer.asm | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/avr/ringbuffer.asm b/avr/ringbuffer.asm index a5bb5c9..a4abbe6 100644 --- a/avr/ringbuffer.asm +++ b/avr/ringbuffer.asm @@ -49,6 +49,9 @@ RingBuffer_Alloc: ldd r18, y+RINGBUFFER_OFFS_USED ldd r19, y+RINGBUFFER_OFFS_WRITEPOS + cpi r16, 2 ; alloc at least two bytes + brcs RingBuffer_Alloc_error ; too few bytes + mov r20, r16 ; to_alloc add r20, r18 ; to_alloc + used cp r20, r17 ; new_used < max? @@ -70,7 +73,8 @@ RingBuffer_Alloc_l1: clr r17 add yl, r16 adc yh, r17 - st y+RINGBUFFER_OFFS_DATA, r17 + std y+RINGBUFFER_OFFS_DATA, r17 ; (length byte in COM module) + std y+RINGBUFFER_OFFS_DATA+1, r17 ; (flags byte in COM module) pop yh pop yl