From 929a02f18556b9f1109c84d784a56de1e36a6e41 Mon Sep 17 00:00:00 2001 From: Dinh Cong Toan Date: Tue, 1 Dec 2020 15:02:27 +0700 Subject: gtest_dlt_common: fix seg fault when using memcpy(). - Checking size of buffer and size of message before using memcpy(). - Adding configuration file for testfilter in /tests/CMakeLists.txt. Signed-off-by: Dinh Cong Toan --- src/shared/dlt_common.c | 14 +++++++++----- tests/CMakeLists.txt | 2 ++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/shared/dlt_common.c b/src/shared/dlt_common.c index 1ae56c1..f8aa746 100644 --- a/src/shared/dlt_common.c +++ b/src/shared/dlt_common.c @@ -2392,9 +2392,11 @@ void dlt_buffer_write_block(DltBuffer *buf, int *write, const unsigned char *dat *write += (int) size; } else { - /* 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)); + if(buf->size > (unsigned int) (*write)) { + /* 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)); + } *write += (int) (size - buf->size); } } @@ -2417,9 +2419,11 @@ void dlt_buffer_read_block(DltBuffer *buf, int *read, unsigned char *data, unsig *read +=(int) size; } else { + if(buf->size > (unsigned int) (*read)) { /* 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)); + 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); } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 53b3949..3b7bbe4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,6 +2,8 @@ add_compile_options(-isystem ${gtest_SOURCE_DIR}/include) configure_file(${PROJECT_SOURCE_DIR}/tests/testfile.dlt ${PROJECT_BINARY_DIR}/tests COPYONLY) +configure_file(${PROJECT_SOURCE_DIR}/tests/testfilter.txt ${PROJECT_BINARY_DIR}/tests COPYONLY) +configure_file(${PROJECT_SOURCE_DIR}/tests/testfile_filetransfer.txt ${PROJECT_BINARY_DIR}/tests COPYONLY) if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") set(LIBRARIES gtest gtest_main) -- cgit v1.2.1