[PATCH for-6.2 2/4] gdbstub: Zero-initialize sockaddr structs

Peter Maydell posted 4 patches 4 years, 5 months ago
Maintainers: Thomas Huth <thuth@redhat.com>, Jason Wang <jasowang@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, Laurent Vivier <lvivier@redhat.com>, "Philippe Mathieu-Daudé" <philmd@redhat.com>, Corey Minyard <minyard@acm.org>, "Alex Bennée" <alex.bennee@linaro.org>
[PATCH for-6.2 2/4] gdbstub: Zero-initialize sockaddr structs
Posted by Peter Maydell 4 years, 5 months ago
Zero-initialize sockaddr_in and sockaddr_un structs that we're about
to fill in and pass to bind() or connect(), to ensure we don't leave
possible implementation-defined extension fields as uninitialized
garbage.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 gdbstub.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/gdbstub.c b/gdbstub.c
index 52bde5bdc97..5d8e6ae3cd9 100644
--- a/gdbstub.c
+++ b/gdbstub.c
@@ -3218,7 +3218,7 @@ static bool gdb_accept_socket(int gdb_fd)
 
 static int gdbserver_open_socket(const char *path)
 {
-    struct sockaddr_un sockaddr;
+    struct sockaddr_un sockaddr = {};
     int fd, ret;
 
     fd = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -3247,7 +3247,7 @@ static int gdbserver_open_socket(const char *path)
 
 static bool gdb_accept_tcp(int gdb_fd)
 {
-    struct sockaddr_in sockaddr;
+    struct sockaddr_in sockaddr = {};
     socklen_t len;
     int fd;
 
-- 
2.20.1


Re: [PATCH for-6.2 2/4] gdbstub: Zero-initialize sockaddr structs
Posted by Eric Blake 4 years, 5 months ago
On Fri, Aug 13, 2021 at 04:05:04PM +0100, Peter Maydell wrote:
> Zero-initialize sockaddr_in and sockaddr_un structs that we're about
> to fill in and pass to bind() or connect(), to ensure we don't leave
> possible implementation-defined extension fields as uninitialized
> garbage.
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>  gdbstub.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/gdbstub.c b/gdbstub.c
> index 52bde5bdc97..5d8e6ae3cd9 100644
> --- a/gdbstub.c
> +++ b/gdbstub.c
> @@ -3218,7 +3218,7 @@ static bool gdb_accept_socket(int gdb_fd)
>  
>  static int gdbserver_open_socket(const char *path)
>  {
> -    struct sockaddr_un sockaddr;
> +    struct sockaddr_un sockaddr = {};

I know we use this non-standard form (which both gcc and clang accept)
because at least older versions of clang needlessly warn on the
standard C construct {0} in some situations, but figuring out when we
no longer support those older compilers and converting our code-base
to look more like standardized code is not my priority.

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org