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,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;