Skip to content

Commit b45f96f

Browse files
committed
sys: ring_buffer: fix possible ring_buf_put_claim() wrong size
- The issue is caused by the MIN() macro, which expands to (a)<(b)?(a):(b), where ring_buf_space_get() is used as 'b' and is evaluated twice. The issue occurs when the (a)<(b) condition evaluates such that (b) is selected, but the value of (b) changes between evaluations, resulting in a possibly larger value than (a). - Fixes the potential incorrect behavior by storing the result of ring_buf_space_get() in a variable before using it in the MIN macro. Signed-off-by: Andrej Butok <[email protected]>
1 parent a4e41f4 commit b45f96f

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

include/zephyr/sys/ring_buffer.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,9 @@ static inline uint32_t ring_buf_put_claim(struct ring_buf *buf,
304304
uint8_t **data,
305305
uint32_t size)
306306
{
307+
uint32_t space = ring_buf_space_get(buf);
307308
return ring_buf_area_claim(buf, &buf->put, data,
308-
MIN(size, ring_buf_space_get(buf)));
309+
MIN(size, space));
309310
}
310311

311312
/**

0 commit comments

Comments
 (0)