From 89e84092441da3a6d5127a681168d4f5ae2f73f9 Mon Sep 17 00:00:00 2001 From: dongjiuzhu1 Date: Sat, 25 Feb 2023 10:41:22 +0800 Subject: [PATCH] mm/circbuf: update buffer head according to real length of writing Signed-off-by: dongjiuzhu1 --- mm/circbuf/circbuf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mm/circbuf/circbuf.c b/mm/circbuf/circbuf.c index 4010b2b1d4..e703902190 100644 --- a/mm/circbuf/circbuf.c +++ b/mm/circbuf/circbuf.c @@ -501,6 +501,7 @@ ssize_t circbuf_overwrite(FAR struct circbuf_s *circ, FAR const void *src, size_t bytes) { size_t overwrite = 0; + size_t skip = 0; size_t space; size_t off; @@ -514,14 +515,15 @@ ssize_t circbuf_overwrite(FAR struct circbuf_s *circ, if (bytes > circ->size) { - src = (FAR const void *)((FAR char *)src + bytes - circ->size); + skip = bytes - circ->size; + src = (FAR const void *)((FAR char *)src + skip); bytes = circ->size; } space = circbuf_space(circ); if (bytes > space) { - overwrite = bytes - space; + overwrite = bytes - space + skip; } off = circ->head % circ->size; @@ -533,7 +535,7 @@ ssize_t circbuf_overwrite(FAR struct circbuf_s *circ, memcpy((FAR char *)circ->base + off, src, space); memcpy(circ->base, (FAR char *)src + space, bytes - space); - circ->head += bytes; + circ->head += bytes + skip; circ->tail += overwrite; return overwrite;