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 +++++++++--- tests/gtest_dlt_common.cpp | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 7 deletions(-) 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); } } } diff --git a/tests/gtest_dlt_common.cpp b/tests/gtest_dlt_common.cpp index 9afb830..4e08d9a 100644 --- a/tests/gtest_dlt_common.cpp +++ b/tests/gtest_dlt_common.cpp @@ -1277,8 +1277,20 @@ TEST(t_dlt_buffer_write_block, normal) } TEST(t_dlt_buffer_write_block, abnormal) { - /* actual no abnormal test cases */ - /* because of void funktion and missing gtest tools for that */ + /* Boundary check of write position */ + DltBuffer buf; + const char *data = "data"; + int write = DLT_USER_RINGBUFFER_MIN_SIZE; + write -= sizeof(DltBufferHead); + int size = sizeof(data); + // when write = buf->size, it should not throw any warning + // and write should equal to size. + EXPECT_LE(DLT_RETURN_OK, + dlt_buffer_init_dynamic(&buf,DLT_USER_RINGBUFFER_MIN_SIZE, DLT_USER_RINGBUFFER_MAX_SIZE, + DLT_USER_RINGBUFFER_STEP_SIZE)); + EXPECT_NO_THROW(dlt_buffer_write_block(&buf, &write, (unsigned char *)&data, size)); + EXPECT_EQ(size , write); + EXPECT_LE(DLT_RETURN_OK, dlt_buffer_free_dynamic(&buf)); } TEST(t_dlt_buffer_write_block, nullpointer) { @@ -1358,8 +1370,23 @@ TEST(t_dlt_buffer_read_block, normal) } TEST(t_dlt_buffer_read_block, abnormal) { - /* actual no abnormal test cases */ - /* because of void funktion and missing gtest tools for that */ + /* Boundary check of read position */ + DltBuffer buf; + /* Buffer to read data from DltBuffer */ + unsigned char *data_read; + data_read = (unsigned char *) calloc(1000, sizeof(char)); + int read = DLT_USER_RINGBUFFER_MIN_SIZE; + read -= sizeof(DltBufferHead); + int size = 1000; + // when read = buf->size, it should not throw any warning + // and read position should equal to size. + EXPECT_LE(DLT_RETURN_OK, + dlt_buffer_init_dynamic(&buf, DLT_USER_RINGBUFFER_MIN_SIZE, DLT_USER_RINGBUFFER_MAX_SIZE, + DLT_USER_RINGBUFFER_STEP_SIZE)); + EXPECT_NO_THROW(dlt_buffer_read_block(&buf, &read, data_read, size)); + EXPECT_EQ(size,read); + EXPECT_LE(DLT_RETURN_OK, dlt_buffer_free_dynamic(&buf)); + free(data_read); } TEST(t_dlt_buffer_read_block, nullpointer) { -- cgit v1.2.1