mm/circbuf: fix the problem of dividing by zero.

N/A

Change-Id: I78a5cae0f782bc2c09848ebda9589dc53809e089
Signed-off-by: dongjiuzhu <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu 2020-11-30 16:24:41 +08:00 committed by Xiang Xiao
parent 756cb41a6c
commit 642718709d

View file

@ -102,12 +102,14 @@ 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);
if (bytes)
{
tmp = kmm_malloc(bytes);
if (!tmp)
{
@ -122,6 +124,7 @@ int circbuf_resize(FAR struct circbuf_s *circ, size_t bytes)
}
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;