diff mbox series

[2/2] src/locktest: add missing struct cast with syscall getsockname()

Message ID 20250304122119.21412-3-luis@igalia.com (mailing list archive)
State New
Headers show
Series Fix compilation errors when not using GNU libc | expand

Commit Message

Luis Henriques March 4, 2025, 12:21 p.m. UTC
The usage of variable 'myAddr' (struct sockaddr_in) is being casted into a
'struct sockaddr' when used with bind() and with connect().  This patch
adds that cast when used with getsockname() as well, otherwise we'll get the
following error:

locktest.c: In function 'main':
locktest.c:1155:39: error: passing argument 2 of 'getsockname' from incompatible pointer type [-Wincompatible-pointer-types]
 1155 |                 if (getsockname(s_fd, &myAddr, &addr_len)) {
      |                                       ^~~~~~~
      |                                       |
      |                                       struct sockaddr_in *
In file included from /usr/include/fortify/sys/socket.h:23,
                 from locktest.c:19:
/usr/include/sys/socket.h:391:23: note: expected 'struct sockaddr * restrict' but argument is of type 'struct sockaddr_in *'
  391 | int getsockname (int, struct sockaddr *__restrict, socklen_t *__restrict);
      |                       ^

Signed-off-by: Luis Henriques <luis@igalia.com>
---
 src/locktest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Zorro Lang March 4, 2025, 1:48 p.m. UTC | #1
On Tue, Mar 04, 2025 at 12:21:19PM +0000, Luis Henriques wrote:
> The usage of variable 'myAddr' (struct sockaddr_in) is being casted into a
> 'struct sockaddr' when used with bind() and with connect().  This patch
> adds that cast when used with getsockname() as well, otherwise we'll get the
> following error:
> 
> locktest.c: In function 'main':
> locktest.c:1155:39: error: passing argument 2 of 'getsockname' from incompatible pointer type [-Wincompatible-pointer-types]
>  1155 |                 if (getsockname(s_fd, &myAddr, &addr_len)) {
>       |                                       ^~~~~~~
>       |                                       |
>       |                                       struct sockaddr_in *
> In file included from /usr/include/fortify/sys/socket.h:23,
>                  from locktest.c:19:
> /usr/include/sys/socket.h:391:23: note: expected 'struct sockaddr * restrict' but argument is of type 'struct sockaddr_in *'
>   391 | int getsockname (int, struct sockaddr *__restrict, socklen_t *__restrict);
>       |                       ^
> 
> Signed-off-by: Luis Henriques <luis@igalia.com>
> ---

From the manual of getsockname:

       int getsockname(int sockfd, struct sockaddr *restrict addr,
                       socklen_t *restrict addrlen);

looks like the 2nd argument is "struct sockaddr *" in GNU libc too.
So I think this patch is good to me.

Reviewed-by: Zorro Lang <zlang@redhat.com>

>  src/locktest.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/src/locktest.c b/src/locktest.c
> index 0e7c3008d1d0..a6cf3b1d5a99 100644
> --- a/src/locktest.c
> +++ b/src/locktest.c
> @@ -1152,7 +1152,7 @@ main(int argc, char *argv[])
>  	if (port == 0) {
>  	        socklen_t addr_len = sizeof(myAddr);
>  
> -		if (getsockname(s_fd, &myAddr, &addr_len)) {
> +		if (getsockname(s_fd, (struct sockaddr *)&myAddr, &addr_len)) {
>  		    perror("getsockname");
>  		    exit(1);
>  		}
>
diff mbox series

Patch

diff --git a/src/locktest.c b/src/locktest.c
index 0e7c3008d1d0..a6cf3b1d5a99 100644
--- a/src/locktest.c
+++ b/src/locktest.c
@@ -1152,7 +1152,7 @@  main(int argc, char *argv[])
 	if (port == 0) {
 	        socklen_t addr_len = sizeof(myAddr);
 
-		if (getsockname(s_fd, &myAddr, &addr_len)) {
+		if (getsockname(s_fd, (struct sockaddr *)&myAddr, &addr_len)) {
 		    perror("getsockname");
 		    exit(1);
 		}