From 0ee024710a49ab3628eba0e7a701131a71d4637f Mon Sep 17 00:00:00 2001
From: Dominik Loidolt <dominik.loidolt@univie.ac.at>
Date: Fri, 21 Oct 2022 17:05:35 +0200
Subject: [PATCH] fix a endianess bug in the cmp_ent_get_end_timestamp()
 function

---
 include/byteorder.h                       | 8 ++++++--
 lib/cmp_entity.c                          | 2 +-
 test/cmp_data_types/test_cmp_data_types.c | 2 +-
 test/cmp_entity/test_cmp_entity.c         | 2 +-
 4 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/include/byteorder.h b/include/byteorder.h
index 42fa9b8..ff25578 100644
--- a/include/byteorder.h
+++ b/include/byteorder.h
@@ -39,7 +39,11 @@
 
 #include <stdint.h>
 
-
+#ifndef GCC_VERSION
+#define GCC_VERSION (__GNUC__ * 10000		\
+		     + __GNUC_MINOR__ * 100	\
+		     + __GNUC_PATCHLEVEL__)
+#endif
 
 #ifdef __BIG_ENDIAN
 #undef __BIG_ENDIAN
@@ -72,7 +76,7 @@
 	(((uint32_t)(x) & (uint32_t)0x00ff0000UL) >>  8) |	\
 	(((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
 
-#define ___constant_swab64(x) ((uint64_t)(			\
+#define ___constant_swab64(x) ((uint64_t)(				\
 	(((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) |	\
 	(((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) |	\
 	(((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) |	\
diff --git a/lib/cmp_entity.c b/lib/cmp_entity.c
index 40900bc..a2a6637 100644
--- a/lib/cmp_entity.c
+++ b/lib/cmp_entity.c
@@ -1083,7 +1083,7 @@ uint64_t cmp_ent_get_end_timestamp(struct cmp_entity *ent)
 		return 0;
 
 #ifdef __LITTLE_ENDIAN
-	return be64_to_cpu(ent->end_timestamp << 16);
+	return be64_to_cpu(ent->end_timestamp) >> 16;
 #else
 	return ent->end_timestamp;
 #endif /* __LITTLE_ENDIAN */
diff --git a/test/cmp_data_types/test_cmp_data_types.c b/test/cmp_data_types/test_cmp_data_types.c
index 0e5252c..4d6d431 100644
--- a/test/cmp_data_types/test_cmp_data_types.c
+++ b/test/cmp_data_types/test_cmp_data_types.c
@@ -51,7 +51,7 @@ void test_cmp_cal_size_of_data(void)
 
 void test_cmp_set_max_used_bits(void)
 {
-	struct cmp_max_used_bits set_max_used_bits;
+	struct cmp_max_used_bits set_max_used_bits = {0};
 	cmp_set_max_used_bits(&set_max_used_bits);
 	cmp_set_max_used_bits(NULL);
 }
diff --git a/test/cmp_entity/test_cmp_entity.c b/test/cmp_entity/test_cmp_entity.c
index db4f658..2122b83 100644
--- a/test/cmp_entity/test_cmp_entity.c
+++ b/test/cmp_entity/test_cmp_entity.c
@@ -328,7 +328,7 @@ void test_ent_end_timestamp(void)
 	TEST_ASSERT_FALSE(error);
 
 	end_timestamp_read = cmp_ent_get_end_timestamp(&ent);
-	TEST_ASSERT_EQUAL_UINT64(end_timestamp, end_timestamp_read);
+	TEST_ASSERT_EQUAL_HEX64(end_timestamp, end_timestamp_read);
 
 	/* check the right position in the header */
 	TEST_ASSERT_EQUAL_HEX(0x12, entity_p[16]);
-- 
GitLab