From 92010a51b525f0d32bf46b15e8e2fe6b1c7da4c4 Mon Sep 17 00:00:00 2001 From: Lutz Helwing Date: Thu, 25 Feb 2016 15:11:32 +0100 Subject: Fix bug with truncation of string or raw block With commit d319ebf4d2d449d70befae0eebbdd82c19c5315f a bug was introduces which lead to some messages getting not transmitted. Change-Id: I9f20177fbc7db9d7caf527e14159444b5fec168a Signed-off-by: Lutz Helwing --- src/lib/dlt_user.c | 79 ++++++++++++++++++++---------------------------- tests/gtest_dlt_user.cpp | 2 +- 2 files changed, 34 insertions(+), 47 deletions(-) diff --git a/src/lib/dlt_user.c b/src/lib/dlt_user.c index b0afdb2..69fb1a4 100644 --- a/src/lib/dlt_user.c +++ b/src/lib/dlt_user.c @@ -1404,6 +1404,7 @@ DltReturnValue dlt_user_log_write_raw(DltContextData *log, void *data, uint16_t DltReturnValue dlt_user_log_write_raw_formatted(DltContextData *log, void *data, uint16_t length, DltFormatType type) { + uint16_t arg_size = 0; uint32_t type_info = 0; size_t new_log_size = 0; @@ -1424,32 +1425,28 @@ DltReturnValue dlt_user_log_write_raw_formatted(DltContextData *log, void *data, return DLT_RETURN_ERROR; } - new_log_size = log->size + sizeof(uint16_t) + length; + arg_size = (uint16_t)length; + new_log_size = log->size + arg_size + sizeof(uint16_t); - /* When data is too large for the buffer it will be truncated to fit */ if (new_log_size > DLT_USER_BUF_MAX_SIZE) - { - length -= new_log_size - DLT_USER_BUF_MAX_SIZE; - new_log_size -= new_log_size - DLT_USER_BUF_MAX_SIZE; - } + arg_size -= new_log_size - DLT_USER_BUF_MAX_SIZE; if (dlt_user.verbose_mode) { - new_log_size += sizeof(uint32_t); + new_log_size = log->size + arg_size + sizeof(uint32_t) + sizeof(uint16_t); - /* When data is too large for the buffer it will be truncated to fit */ if (new_log_size > DLT_USER_BUF_MAX_SIZE) - length -= new_log_size - DLT_USER_BUF_MAX_SIZE; + arg_size -= new_log_size - DLT_USER_BUF_MAX_SIZE; /* Transmit type information */ type_info = DLT_TYPE_INFO_RAWD; - if(type>=DLT_FORMAT_HEX8 && type<=DLT_FORMAT_HEX64) + if (type >= DLT_FORMAT_HEX8 && type <= DLT_FORMAT_HEX64) { type_info |= DLT_SCOD_HEX; type_info += type; } - else if(type>=DLT_FORMAT_BIN8 && type<=DLT_FORMAT_BIN16) + else if (type >= DLT_FORMAT_BIN8 && type <= DLT_FORMAT_BIN16) { type_info |= DLT_SCOD_BIN; type_info += type - DLT_FORMAT_BIN8 + 1; @@ -1460,12 +1457,11 @@ DltReturnValue dlt_user_log_write_raw_formatted(DltContextData *log, void *data, } /* First transmit length of raw data, then the raw data itself */ - - memcpy((log->buffer) + log->size, &(length), sizeof(uint16_t)); + memcpy((log->buffer) + log->size, &(arg_size), sizeof(uint16_t)); log->size += sizeof(uint16_t); - memcpy((log->buffer) + log->size, data, length); - log->size += length; + memcpy((log->buffer) + log->size, data, arg_size); + log->size += arg_size; log->args_num++; @@ -2223,7 +2219,7 @@ DltReturnValue dlt_user_log_write_bool(DltContextData *log, uint8_t data) DltReturnValue dlt_user_log_write_string(DltContextData *log, const char *text) { - uint16_t text_len = 0; + uint16_t arg_size = 0; uint32_t type_info = 0; size_t new_log_size = 0; @@ -2236,23 +2232,19 @@ DltReturnValue dlt_user_log_write_string(DltContextData *log, const char *text) return DLT_RETURN_ERROR; } - text_len = strlen(text); - new_log_size = log->size + sizeof(uint16_t) + text_len + 1; + arg_size = strlen(text) + 1; + + new_log_size = log->size + arg_size + sizeof(uint16_t); - /* When data is too large for the buffer, text will be truncated to fit */ if (new_log_size > DLT_USER_BUF_MAX_SIZE) - { - text_len -= new_log_size - DLT_USER_BUF_MAX_SIZE; - new_log_size -= new_log_size - DLT_USER_BUF_MAX_SIZE; - } + arg_size -= new_log_size - DLT_USER_BUF_MAX_SIZE; if (dlt_user.verbose_mode) { - new_log_size += sizeof(uint32_t); + new_log_size = log->size + arg_size + sizeof(uint32_t) + sizeof(uint16_t); - /* When data is too large for the buffer, text will be truncated to fit */ if (new_log_size > DLT_USER_BUF_MAX_SIZE) - text_len -= new_log_size - DLT_USER_BUF_MAX_SIZE; + arg_size -= new_log_size - DLT_USER_BUF_MAX_SIZE; type_info = DLT_TYPE_INFO_STRG | DLT_SCOD_ASCII; @@ -2260,15 +2252,15 @@ DltReturnValue dlt_user_log_write_string(DltContextData *log, const char *text) log->size += sizeof(uint32_t); } - memcpy((log->buffer) + log->size, &(text_len), sizeof(uint16_t)); + memcpy((log->buffer) + log->size, &(arg_size), sizeof(uint16_t)); log->size += sizeof(uint16_t); - memcpy((log->buffer) + log->size, text, text_len); - log->size += text_len; + memcpy((log->buffer) + log->size, text, arg_size - 1); + log->size += arg_size - 1; /* Of course, buffer has to be 0-terminated here */ log->buffer[log->size] = '\000'; - log->size += 1; + log->size++; log->args_num++; @@ -2283,8 +2275,8 @@ DltReturnValue dlt_user_log_write_constant_string(DltContextData *log, const cha DltReturnValue dlt_user_log_write_utf8_string(DltContextData *log, const char *text) { - uint16_t text_len = 0; - uint32_t type_info = 0; + uint16_t arg_size; + uint32_t type_info; size_t new_log_size = 0; if (log == NULL || text == NULL) @@ -2296,23 +2288,18 @@ DltReturnValue dlt_user_log_write_utf8_string(DltContextData *log, const char *t return DLT_RETURN_ERROR; } - text_len = strlen(text); - new_log_size = log->size + sizeof(uint16_t) + text_len + 1; + arg_size = strlen(text) + 1; + new_log_size = log->size + arg_size + sizeof(uint16_t); - /* When data is too large for the buffer, text will be truncated to fit */ if (new_log_size > DLT_USER_BUF_MAX_SIZE) - { - text_len -= new_log_size - DLT_USER_BUF_MAX_SIZE; - new_log_size -= new_log_size - DLT_USER_BUF_MAX_SIZE; - } + arg_size -= new_log_size - DLT_USER_BUF_MAX_SIZE; if (dlt_user.verbose_mode) { - new_log_size += sizeof(uint32_t); + new_log_size = log->size + arg_size + sizeof(uint32_t) + sizeof(uint16_t); - /* When data is too large for the buffer, text will be truncated to fit */ if (new_log_size > DLT_USER_BUF_MAX_SIZE) - text_len -= new_log_size - DLT_USER_BUF_MAX_SIZE; + arg_size -= new_log_size - DLT_USER_BUF_MAX_SIZE; type_info = DLT_TYPE_INFO_STRG | DLT_SCOD_UTF8; @@ -2320,15 +2307,15 @@ DltReturnValue dlt_user_log_write_utf8_string(DltContextData *log, const char *t log->size += sizeof(uint32_t); } - memcpy((log->buffer) + log->size, &(text_len), sizeof(uint16_t)); + memcpy((log->buffer) + log->size, &(arg_size), sizeof(uint16_t)); log->size += sizeof(uint16_t); - memcpy((log->buffer) + log->size, text, text_len); - log->size += text_len; + memcpy((log->buffer) + log->size, text, arg_size - 1); + log->size += arg_size - 1; /* Of course, buffer has to be 0-terminated here */ log->buffer[log->size] = '\000'; - log->size += 1; + log->size++; log->args_num++; diff --git a/tests/gtest_dlt_user.cpp b/tests/gtest_dlt_user.cpp index c7d8008..6f77b6e 100644 --- a/tests/gtest_dlt_user.cpp +++ b/tests/gtest_dlt_user.cpp @@ -2188,7 +2188,7 @@ TEST(t_dlt_log_raw, abnormal) // undefined values for DltLogLevelType // shouldn't it return -1? - char data[5] = "test"; +// char data[5] = "test"; // TODO: uint16_t length = 4; // TODO: EXPECT_GE(DLT_RETURN_ERROR,dlt_log_raw(&context, (DltLogLevelType)-100, data, length)); // TODO: EXPECT_GE(DLT_RETURN_ERROR,dlt_log_raw(&context, (DltLogLevelType)-10, data, length)); -- cgit v1.2.1