[PATCH] osmo-ggsn[master]: sanitize build: ensure uint16/32 alignment in gtpie_test and...

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Sat Nov 18 17:33:39 UTC 2017


Review at  https://gerrit.osmocom.org/4915

sanitize build: ensure uint16/32 alignment in gtpie_test and in46a_test

Fixes sanitize build failures:

  Testing gtpie_tlv()
  ../../../../src/osmo-ggsn/tests/gtp/gtpie_test.c:30:2: runtime error: load of misaligned address 0x55c0a0830f21 for type 'uint16_t', which requires 2 byte alignment
  0x55c0a0830f21: note: pointer points here
   00 00 00  17 00 06 01 02 03 04 05  06 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
                ^
  Testing gtpie_tv0()
  Testing gtpie_tv1()
  Testing gtpie_tv2()
  ../../../../src/osmo-ggsn/tests/gtp/gtpie_test.c:76:2: runtime error: load of misaligned address 0x55c0a0830f21 for type 'uint16_t', which requires 2 byte alignment
  0x55c0a0830f21: note: pointer points here
   00 00 00  2a ab cd 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
                ^
  Testing gtpie_tv4()
  ../../../../src/osmo-ggsn/tests/gtp/gtpie_test.c:90:2: runtime error: load of misaligned address 0x55c0a0830f21 for type 'uint32_t', which requires 4 byte alignment
  0x55c0a0830f21: note: pointer points here
   00 00 00  2a ab cd 01 23 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
                ^
  Testing gtpie_tv8()
  ../../../../src/osmo-ggsn/tests/gtp/gtpie_test.c:104:2: runtime error: load of misaligned address 0x55c0a0830f21 for type 'uint32_t', which requires 4 byte alignment
  0x55c0a0830f21: note: pointer points here
   00 00 00  2a 00 01 02 03 04 05 06  07 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
                ^
  ../../../../src/osmo-ggsn/tests/gtp/gtpie_test.c:105:2: runtime error: load of misaligned address 0x55c0a0830f25 for type 'uint32_t', which requires 4 byte alignment
  0x55c0a0830f25: note: pointer points here
   00 01 02 03 04 05 06  07 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00
               ^

Change-Id: I9eb16450af942d6464211e190f6a4d5a1d814842
---
M tests/gtp/gtpie_test.c
M tests/lib/in46a_test.c
2 files changed, 22 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-ggsn refs/changes/15/4915/1

diff --git a/tests/gtp/gtpie_test.c b/tests/gtp/gtpie_test.c
index 8d87dcc..97a2bdc 100644
--- a/tests/gtp/gtpie_test.c
+++ b/tests/gtp/gtpie_test.c
@@ -18,6 +18,7 @@
 static void test_gtpie_tlv()
 {
 	unsigned int len = 0;
+	uint16_t u16;
 
 	printf("Testing gtpie_tlv()\n");
 
@@ -27,7 +28,9 @@
 	OSMO_ASSERT(rc == 0);
 	OSMO_ASSERT(len == sizeof(in) + 3);
 	OSMO_ASSERT(buf[0] == 23);
-	OSMO_ASSERT(ntohs(*(uint16_t *) &buf[1]) == sizeof(in));
+	/* uint16_t must be 2-byte aligned to not trigger sanitize build failure */
+	memcpy(&u16, &buf[1], sizeof(u16));
+	OSMO_ASSERT(ntohs(u16) == sizeof(in));
 	OSMO_ASSERT(!memcmp(buf+3, in, sizeof(in)));
 
 	/* overflow */
@@ -65,6 +68,7 @@
 static void test_gtpie_tv2()
 {
 	unsigned int len = 0;
+	uint16_t u16;
 
 	printf("Testing gtpie_tv2()\n");
 
@@ -73,12 +77,15 @@
 	OSMO_ASSERT(rc == 0);
 	OSMO_ASSERT(len == 3);
 	OSMO_ASSERT(buf[0] == 42);
-	OSMO_ASSERT(ntohs(*(uint16_t *) &buf[1]) == 0xABCD);
+	/* uint16_t must be 2-byte aligned to not trigger sanitize build failure */
+	memcpy(&u16, &buf[1], sizeof(u16));
+	OSMO_ASSERT(ntohs(u16) == 0xABCD);
 }
 
 static void test_gtpie_tv4()
 {
 	unsigned int len = 0;
+	uint32_t u32;
 
 	printf("Testing gtpie_tv4()\n");
 
@@ -87,12 +94,15 @@
 	OSMO_ASSERT(rc == 0);
 	OSMO_ASSERT(len == 5);
 	OSMO_ASSERT(buf[0] == 42);
-	OSMO_ASSERT(ntohl(*(uint32_t *) &buf[1]) == 0xABCD0123);
+	/* uint32_t must be 4-byte aligned to not trigger sanitize build failure */
+	memcpy(&u32, &buf[1], sizeof(u32));
+	OSMO_ASSERT(ntohl(u32) == 0xABCD0123);
 }
 
 static void test_gtpie_tv8()
 {
 	unsigned int len = 0;
+	uint32_t u32;
 
 	printf("Testing gtpie_tv8()\n");
 
@@ -101,8 +111,11 @@
 	OSMO_ASSERT(rc == 0);
 	OSMO_ASSERT(len == 9);
 	OSMO_ASSERT(buf[0] == 42);
-	OSMO_ASSERT(ntohl(*(uint32_t *) &buf[1]) == 0x00010203);
-	OSMO_ASSERT(ntohl(*(uint32_t *) &buf[5]) == 0x04050607);
+	/* uint32_t must be 4-byte aligned to not trigger sanitize build failure */
+	memcpy(&u32, &buf[1], sizeof(u32));
+	OSMO_ASSERT(ntohl(u32) == 0x00010203);
+	memcpy(&u32, &buf[5], sizeof(u32));
+	OSMO_ASSERT(ntohl(u32) == 0x04050607);
 }
 
 int main(int argc, char **argv)
diff --git a/tests/lib/in46a_test.c b/tests/lib/in46a_test.c
index ab7156f..bca1015 100644
--- a/tests/lib/in46a_test.c
+++ b/tests/lib/in46a_test.c
@@ -159,6 +159,7 @@
 		.v6.s6_addr = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 },
 	};
 	struct ul66_t eua;
+	uint32_t v4_addr;
 
 	printf("testing in46a_to_eua()\n");
 
@@ -171,7 +172,9 @@
 	OSMO_ASSERT(in46a_to_eua(&g_ia4, &eua) == 0);
 	OSMO_ASSERT(eua.v[0] == PDP_EUA_ORG_IETF);
 	OSMO_ASSERT(eua.v[1] == PDP_EUA_TYPE_v4);
-	OSMO_ASSERT(*(uint32_t *) &eua.v[2] == g_ia4.v4.s_addr);
+	/* uint32_t must be 4-byte aligned to not trigger sanitize build failure */
+	memcpy(&v4_addr, &eua.v[2], sizeof(v4_addr));
+	OSMO_ASSERT(v4_addr == g_ia4.v4.s_addr);
 
 	/* IPv6 address */
 	OSMO_ASSERT(in46a_to_eua(&g_ia6, &eua) == 0);

-- 
To view, visit https://gerrit.osmocom.org/4915
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9eb16450af942d6464211e190f6a4d5a1d814842
Gerrit-PatchSet: 1
Gerrit-Project: osmo-ggsn
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>



More information about the gerrit-log mailing list