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 5f01b4a0fdafe0785bb4dac4164ef6eecd338abd Author: Bowen Wang <[email protected]> AuthorDate: Fri May 15 23:27:47 2026 +0800 include/ring: guard ring_copy_from/to against zero-count access When the ring is full (enqueue) or empty (dequeue), the start functions return buf->cnt == 0. Previously ring_copy_from and ring_copy_to would fall into the else branch and call memcpy with len1=0 and ptr1=NULL, which is undefined behavior per the C standard even when size is zero. Change the else branch to else-if (buf->cnt != 0) so that zero-count cases are safely skipped without adding an extra early-return branch. Signed-off-by: Bowen Wang <[email protected]> --- include/nuttx/ring.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nuttx/ring.h b/include/nuttx/ring.h index 76a0c5248c5..044147bf490 100644 --- a/include/nuttx/ring.h +++ b/include/nuttx/ring.h @@ -687,7 +687,7 @@ ring_copy_from(FAR struct ring_buf_s *buf, FAR const void *items, memcpy(buf->ptr1, items, elem_size); } } - else + else if (buf->cnt != 0) { memcpy(buf->ptr1, items, buf->len1 * elem_size); if (buf->len2 != 0) @@ -732,7 +732,7 @@ ring_copy_to(FAR struct ring_buf_s *buf, FAR void *items, size_t elem_size) memcpy(items, buf->ptr1, elem_size); } } - else + else if (buf->cnt != 0) { memcpy(items, buf->ptr1, buf->len1 * elem_size); if (buf->len2 != 0)
