This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 2cc4b639f0506133842eec0a28712204c8d0bf93 Author: wangjianyu3 <[email protected]> AuthorDate: Wed Jul 8 20:13:52 2026 +0800 include/nuttx/ring: clamp torn-snapshot dequeue length in overwrite rings ring_sc_start()/ring_mc_start() derive the available element count from a non-atomic pair of index reads: prod_tail is loaded separately from the consumer index. In OVERWRITE mode the producer is allowed to advance the consumer-owned cons_head/cons_tail to evict the oldest entries, so a flush consumer can observe a torn snapshot where prod_tail is stale while the consumer index has already been pushed past it in 24-bit modular order. ring_dist() then computes (prod_stale - cons_new) & RING_IDX_MASK, which underflows to ~16M - far larger than the ring capacity. n = min(count, available) stays huge and ring_buf_init() faithfully splits it into len1 + len2 with no capacity clamp, producing an over-length segment. The downstream memcpy in the note/netpoll path then walks off the mapped ring and takes a bus trap. A consistent ring can never hold more committed entries than its capacity, so any available > capacity is by definition a torn read. Force available to 0 in that case: the round yields no data and the periodic watermark/timeout flush re-reads a coherent snapshot on the next pass with no entries lost. Clamping to 0 rather than capacity avoids advancing the consumer index past the real prod_tail, which would corrupt the ring. The same producer/consumer divergence also underflows the producer side. ring_mp_overwrite_start() computes free_space = capacity - ring_dist( prod_head, cons_head); once the producer laps the consumer by more than capacity in 24-bit modular order this subtraction wraps to ~4G. The "free_space < count" test is then never true, so the eviction branch that reclaims the oldest slots is never entered again: the producer advances prod_head without bound and the evicted-byte accounting silently stops, leaving megabytes of overwritten data uncounted. Guard the subtraction to yield 0 when the ring is already full, forcing the eviction path to run and restoring the invariant dist(prod_head, cons_head) <= capacity. Signed-off-by: wangjianyu3 <[email protected]> --- include/nuttx/ring.h | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/include/nuttx/ring.h b/include/nuttx/ring.h index a8ac80bab2d..3be700fa83d 100644 --- a/include/nuttx/ring.h +++ b/include/nuttx/ring.h @@ -980,12 +980,23 @@ ring_sc_start(FAR struct ring_s *r, uint32_t count, uint32_t cons_index = RTS_IDX(RING_FIELD(&r->cons_tail)); uint32_t prod_index; uint32_t available; - uint32_t n; + uint32_t n = 0; UP_RMB(); prod_index = RTS_IDX(RING_FIELD(&r->prod_tail)); available = (prod_index - cons_index) & RING_IDX_MASK; - n = count < available ? count : available; + + /* available > capacity is physically impossible for a consistent ring; + * it can only result from a torn snapshot where prod_tail is stale and + * the consumer indices were advanced past it by an overwrite eviction. + * Treat it as no data this round; the next pass re-reads a coherent + * snapshot without losing entries. + */ + + if (available <= r->capacity) + { + n = count < available ? count : available; + } ring_buf_init(buf, r, cons_index, n, elem_size); } @@ -1014,6 +1025,19 @@ ring_mc_start(FAR struct ring_s *r, uint32_t count, uint32_t available = (prod_index - RTS_IDX(old_head)) & RING_IDX_MASK; + /* available > capacity is physically impossible for a consistent + * ring; it can only result from a torn snapshot where prod_tail is + * stale and the consumer indices were advanced past it by an + * overwrite eviction. Treat it as no data this round; the next pass + * re-reads a coherent snapshot without losing entries. + */ + + if (available > r->capacity) + { + n = 0; + break; + } + n = count < available ? count : available; if (n == 0) { @@ -1638,8 +1662,9 @@ ring_mp_overwrite_start(FAR struct ring_s *r, uint32_t count, prod_idx = RTS_IDX(old_prod); cons_idx = RTS_IDX(old_cons); - free_space = r->capacity - - ((prod_idx - cons_idx) & RING_IDX_MASK); + free_space = (prod_idx - cons_idx) & RING_IDX_MASK; + free_space = free_space < r->capacity ? + r->capacity - free_space : 0; if (free_space < count) {
