Skip to contentSkip to conversation

selftests/bpf: Fix endianness issue in test_sockopt_sk

PATCH Applied Ilya Leoshkevich 4 participants 4 messages Sep 16, 2020 – Sep 18, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftests/bpf: Fix endianness issue in test_sockopt_sk

getsetsockopt() calls getsockopt() with optlen == 1, but then checks the resulting int. It is ok on little endian, but not on big endian.

Fix by checking char instead.

Fixes 8a027dc0 ("selftests/bpf: add sockopt test that exercises sk helpers")

---

v1->v2: Also pass a single byte to log_err.

 tools/testing/selftests/bpf/prog_tests/sockopt_sk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +2 -2
index 5f54c6aec7f0..b25c9c45c148 100644
@@ -45,9 +45,9 @@ static int getsetsockopt(void)
 		goto err;
 	}
 
-	if (*(int *)big_buf != 0x08) {
+	if (*big_buf != 0x08) {
 		log_err("Unexpected getsockopt(IP_TOS) optval 0x%x != 0x08",
-			*(int *)big_buf);
+			(int)*big_buf);
 		goto err;
 	}
 
Signature · 1 line
2.25.4
Acked-by on v2 1/1
#1 lore raw
Quoted 4 lines · Ilya Leoshkevich

getsetsockopt() calls getsockopt() with optlen == 1, but then checks the resulting int. It is ok on little endian, but not on big endian.

Quoted 8 lines

Fix by checking char instead.

Fixes 8a027dc0 ("selftests/bpf: add sockopt test that exercises sk helpers")

---

v1->v2: Also pass a single byte to log_err.

Quoted 22 lines
 tools/testing/selftests/bpf/prog_tests/sockopt_sk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +2 -3
index 5f54c6aec7f0..b25c9c45c148 100644
@@ -45,9 +45,9 @@ static int getsetsockopt(void)
                goto err;
        }
 
-       if (*(int *)big_buf != 0x08) {
+       if (*big_buf != 0x08) {
                log_err("Unexpected getsockopt(IP_TOS) optval 0x%x != 0x08",
-                       *(int *)big_buf);
+                       (int)*big_buf);
                goto err;
        }
 
--

2.25.4

reviewer Acked-by
#2 lore raw
Quoted 8 lines · Andrii Nakryiko
Quoted 6 lines · Ilya Leoshkevich

getsetsockopt() calls getsockopt() with optlen == 1, but then checks the resulting int. It is ok on little endian, but not on big endian.

Fix by checking char instead.

Quoted 8 lines
Quoted 6 lines
Fixes 8a027dc0 ("selftests/bpf: add sockopt test that exercises sk helpers")

---

v1->v2: Also pass a single byte to log_err.