summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMartin Willers <M.Willers@gmx.net>2020-11-17 01:27:36 +0100
committerGitHub <noreply@github.com>2020-11-17 09:27:36 +0900
commite584855b2289fd8155b837f00f67343cc9cd8f66 (patch)
tree81782f1406d059cffac71c42801aa10e19a2af0e /tests
parent347f25f267538773e9d2591cc1d55aebfe8f998f (diff)
downloadDLT-daemon-e584855b2289fd8155b837f00f67343cc9cd8f66.tar.gz
Avoid memory access errors with 4-chars context ids (#250)
For a 4-chars CTXID, i.e. one that does not include a null character, the strlen() calls were happily accessing memory until they eventually encountered a null character somewhere in memory. This was detected by valgrind, which reported a memory error when using a CTXID such as "INTM": ==21924== Conditional jump or move depends on uninitialised value(s) ==21924== at 0x4C30F78: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==21924== by 0x4E4B5A0: dlt_print_id (dlt_common.c:303) ==21924== by 0x4E4CF47: dlt_message_header_flags (dlt_common.c:687) ==21924== by 0x4E51434: dlt_message_print_ascii (dlt_common.c:3169) ==21924== by 0x4E4247A: dlt_user_print_msg (dlt_user.c:4108) ==21924== by 0x4E46D92: dlt_user_log_send_log (dlt_user.c:3670) ==21924== by 0x4E46F14: dlt_user_log_write_finish (dlt_user.c:1611) Sanitize some code Using memset() and memcpy() is always preferable to hand-rolled loops, because compilers have built-in support for them. Signed-off-by: Martin Willers <M.Willers@gmx.net>
Diffstat (limited to 'tests')
-rw-r--r--tests/gtest_dlt_common.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/gtest_dlt_common.cpp b/tests/gtest_dlt_common.cpp
index adbc87d..9afb830 100644
--- a/tests/gtest_dlt_common.cpp
+++ b/tests/gtest_dlt_common.cpp
@@ -4067,6 +4067,49 @@ TEST(t_dlt_print_char_string, nullpointer)
+/* Begin Method:dlt_common::dlt_strnlen_s*/
+TEST(t_dlt_strnlen_s, nullpointer)
+{
+ size_t len = dlt_strnlen_s(NULL, 0);
+ EXPECT_EQ(len, 0);
+}
+
+TEST(t_dlt_strnlen_s, len_zero)
+{
+ const char text[] = "The Quick Brown Fox";
+
+ size_t len = dlt_strnlen_s(text, 0);
+ EXPECT_EQ(len, 0);
+}
+
+TEST(t_dlt_strnlen_s, len_smaller)
+{
+ const char text[] = "The Quick Brown Fox";
+
+ size_t len = dlt_strnlen_s(text, 10);
+ EXPECT_EQ(len, 10);
+}
+
+TEST(t_dlt_strnlen_s, len_equal)
+{
+ const char text[] = "The Quick Brown Fox";
+
+ size_t len = dlt_strnlen_s(text, 19);
+ EXPECT_EQ(len, 19);
+}
+
+TEST(t_dlt_strnlen_s, len_larger)
+{
+ const char text[] = "The Quick Brown Fox";
+
+ size_t len = dlt_strnlen_s(text, 100);
+ EXPECT_EQ(len, 19);
+}
+/* End Method:dlt_common::dlt_strnlen_s*/
+
+
+
+
/* Begin Method:dlt_common::dlt_print_id */
TEST(t_dlt_print_id, normal)
{