From 05e4ad2339a24bcf38663eca37a5292a4038d423 Mon Sep 17 00:00:00 2001 From: Dinh Cong Toan Date: Fri, 18 Jun 2021 13:10:49 +0700 Subject: dlt_common: correct read/write position In dlt_buffer_read_block() and dlt_buffer_write_block(), read/write position should be updated in case these positions are equal to the size of ringbuffer. Adding boundary check in gtest_dlt_common.cpp Signed-off-by: Dinh Cong Toan --- src/shared/dlt_common.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/shared/dlt_common.c b/src/shared/dlt_common.c index 8757296..c597468 100644 --- a/src/shared/dlt_common.c +++ b/src/shared/dlt_common.c @@ -2431,7 +2431,10 @@ void dlt_buffer_write_block(DltBuffer *buf, int *write, const unsigned char *dat *write += (int) size; } else { - if(buf->size > (unsigned int) (*write)) { + /* when (*write) = buf->size, write only the second block + * and update write position correspondingly. + */ + if((unsigned int) (*write) <= buf->size) { /* write two blocks */ memcpy(buf->mem + *write, data, buf->size - (unsigned int) (*write)); memcpy(buf->mem, data + buf->size - *write, size - buf->size + (unsigned int) (*write)); @@ -2458,11 +2461,14 @@ void dlt_buffer_read_block(DltBuffer *buf, int *read, unsigned char *data, unsig *read += (int)size; } else { - if (buf->size > (unsigned int)(*read)) { + /* when (*read) = buf->size, read only the second block + * and update read position correspondingly. + */ + if ((unsigned int)(*read) <= buf->size) { /* read two blocks */ memcpy(data, buf->mem + *read, buf->size - (unsigned int)(*read)); memcpy(data + buf->size - *read, buf->mem, size - buf->size + (unsigned int)(*read)); - *read += (int)(size - buf->size); + *read += (int) (size - buf->size); } } } -- cgit v1.2.1