From 642718709df59b81f7c21c0cf780bc97c1f8217b Mon Sep 17 00:00:00 2001 From: dongjiuzhu Date: Mon, 30 Nov 2020 16:24:41 +0800 Subject: [PATCH] mm/circbuf: fix the problem of dividing by zero. N/A Change-Id: I78a5cae0f782bc2c09848ebda9589dc53809e089 Signed-off-by: dongjiuzhu --- mm/circbuf/circbuf.c | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/mm/circbuf/circbuf.c b/mm/circbuf/circbuf.c index cef8e13692..0912e524e7 100644 --- a/mm/circbuf/circbuf.c +++ b/mm/circbuf/circbuf.c @@ -102,26 +102,29 @@ int circbuf_init(FAR struct circbuf_s *circ, FAR void *base, size_t bytes) int circbuf_resize(FAR struct circbuf_s *circ, size_t bytes) { - FAR void *tmp; - size_t len; + FAR void *tmp = NULL; + size_t len = 0; DEBUGASSERT(circ); DEBUGASSERT(!circ->external); - tmp = kmm_malloc(bytes); - if (!tmp) + if (bytes) { - return -ENOMEM; - } + tmp = kmm_malloc(bytes); + if (!tmp) + { + return -ENOMEM; + } - len = circbuf_used(circ); - if (bytes < len) - { - circbuf_skip(circ, len - bytes); - len = bytes; - } + len = circbuf_used(circ); + if (bytes < len) + { + circbuf_skip(circ, len - bytes); + len = bytes; + } - circbuf_read(circ, tmp, len); + circbuf_read(circ, tmp, len); + } kmm_free(circ->base); @@ -276,6 +279,11 @@ ssize_t circbuf_peek(FAR struct circbuf_s *circ, DEBUGASSERT(circ); + if (!circ->size) + { + return 0; + } + len = circbuf_used(circ); off = circ->tail % circ->size; @@ -394,6 +402,11 @@ ssize_t circbuf_write(FAR struct circbuf_s *circ, DEBUGASSERT(circ); DEBUGASSERT(src || !bytes); + if (!circ->size) + { + return 0; + } + space = circbuf_space(circ); off = circ->head % circ->size; if (bytes > space) @@ -446,6 +459,11 @@ ssize_t circbuf_overwrite(FAR struct circbuf_s *circ, DEBUGASSERT(circ); DEBUGASSERT(src || !bytes); + if (!circ->size) + { + return 0; + } + if (bytes > circ->size) { src += bytes - circ->size;