diff mbox series

[bpf-next,v1,03/19] selftests/bpf: Fix error compiling bpf_iter_setsockopt.c with musl libc

Message ID f41def0f17b27a23b1709080e4e3f37f4cc11ca9.1721713597.git.tony.ambardar@gmail.com (mailing list archive)
State Accepted
Commit 7b10f0c227ce3fa055d601f058dc411092a62a78
Headers show
Series selftests/bpf: Improve libc portability / musl support (part 1) | expand

Commit Message

Tony Ambardar July 23, 2024, 5:54 a.m. UTC
Existing code calls getsockname() with a 'struct sockaddr_in6 *' argument
where a 'struct sockaddr *' argument is declared, yielding compile errors
when building for mips64el/musl-libc:

  bpf_iter_setsockopt.c: In function 'get_local_port':
  bpf_iter_setsockopt.c:98:30: error: passing argument 2 of 'getsockname' from incompatible pointer type [-Werror=incompatible-pointer-types]
     98 |         if (!getsockname(fd, &addr, &addrlen))
        |                              ^~~~~
        |                              |
        |                              struct sockaddr_in6 *
  In file included from .../netinet/in.h:10,
                   from .../arpa/inet.h:9,
                   from ./test_progs.h:17,
                   from bpf_iter_setsockopt.c:5:
  .../sys/socket.h:391:23: note: expected 'struct sockaddr * restrict' but argument is of type 'struct sockaddr_in6 *'
    391 | int getsockname (int, struct sockaddr *__restrict, socklen_t *__restrict);
        |                       ^
  cc1: all warnings being treated as errors

This compiled under glibc only because the argument is declared to be a
"funky" transparent union which includes both types above. Explicitly cast
the argument to allow compiling for both musl and glibc.

Fixes: eed92afdd14c ("bpf: selftest: Test batching and bpf_(get|set)sockopt in bpf tcp iter")
Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com>
---
 tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Geliang Tang July 24, 2024, 3:11 a.m. UTC | #1
On Mon, 2024-07-22 at 22:54 -0700, Tony Ambardar wrote:
> Existing code calls getsockname() with a 'struct sockaddr_in6 *'
> argument
> where a 'struct sockaddr *' argument is declared, yielding compile
> errors
> when building for mips64el/musl-libc:
> 
>   bpf_iter_setsockopt.c: In function 'get_local_port':
>   bpf_iter_setsockopt.c:98:30: error: passing argument 2 of
> 'getsockname' from incompatible pointer type [-Werror=incompatible-
> pointer-types]
>      98 |         if (!getsockname(fd, &addr, &addrlen))
>         |                              ^~~~~
>         |                              |
>         |                              struct sockaddr_in6 *
>   In file included from .../netinet/in.h:10,
>                    from .../arpa/inet.h:9,
>                    from ./test_progs.h:17,
>                    from bpf_iter_setsockopt.c:5:
>   .../sys/socket.h:391:23: note: expected 'struct sockaddr *
> restrict' but argument is of type 'struct sockaddr_in6 *'
>     391 | int getsockname (int, struct sockaddr *__restrict,
> socklen_t *__restrict);
>         |                       ^
>   cc1: all warnings being treated as errors
> 
> This compiled under glibc only because the argument is declared to be
> a
> "funky" transparent union which includes both types above. Explicitly
> cast
> the argument to allow compiling for both musl and glibc.
> 
> Fixes: eed92afdd14c ("bpf: selftest: Test batching and
> bpf_(get|set)sockopt in bpf tcp iter")
> Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com>
> ---
>  tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git
> a/tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c
> b/tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c
> index b52ff8ce34db..35363b104dd2 100644
> --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c
> +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c
> @@ -95,7 +95,7 @@ static unsigned short get_local_port(int fd)
>  	struct sockaddr_in6 addr;
>  	socklen_t addrlen = sizeof(addr);
>  
> -	if (!getsockname(fd, &addr, &addrlen))
> +	if (!getsockname(fd, (struct sockaddr *) &addr, &addrlen))

nit: Generally, a space is not required when casting:

	(struct sockaddr *)&addr
	
	not

	(struct sockaddr *) &addr.

See here:

$ grep -r "struct sockaddr \*) \&"
tools/testing/selftests/bpf/prog_tests | wc -l
1
$ grep -r "struct sockaddr \*)\&"
tools/testing/selftests/bpf/prog_tests | wc -l
33

Except this, LGTM.

Acked-by: Geliang Tang <geliang@kernel.org>

>  		return ntohs(addr.sin6_port);
>  
>  	return 0;
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c
index b52ff8ce34db..35363b104dd2 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt.c
@@ -95,7 +95,7 @@  static unsigned short get_local_port(int fd)
 	struct sockaddr_in6 addr;
 	socklen_t addrlen = sizeof(addr);
 
-	if (!getsockname(fd, &addr, &addrlen))
+	if (!getsockname(fd, (struct sockaddr *) &addr, &addrlen))
 		return ntohs(addr.sin6_port);
 
 	return 0;