net/socket: fix coverity issue
Subsystems (2)
PATCH 1/1 net/socket: fix coverity issue
This fixes coverity issue CID1005339.
Make sure that saddr is not used uninitialized if the mcast parameter is NULL.
--- net/socket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/socket.c +2 -2
@@ -332,7 +332,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,const char *mcast, Error **errp) { - struct sockaddr_in saddr; + struct sockaddr_in saddr = { 0 };int newfd;NetClientState *nc;NetSocketState *s;
@@ -373,7 +373,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,net_socket_read_poll (s, true);/* mcast: save bound address as dst */ -if (is_connected) { +if (is_connected && mcast !=NULL ) { s->dgram_dst = saddr; snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d (cloned mcast=%s:%d)",
Signature · 1 line
2.13.6
Quoted 17 lines · Jens Freimann
This fixes coverity issue CID1005339.
Make sure that saddr is not used uninitialized if the mcast parameter is NULL.
Reported-by Peter Maydell <[email protected]>Signed-off-by Jens Freimann <[email protected]>--- net/socket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)net/socket.c +0
-0
net/socket.c static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, · line 335
Quoted 5 lines
net/socket.c +1
-1@@ -332,7 +332,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,const char *mcast, Error **errp) { - struct sockaddr_in saddr; + struct sockaddr_in saddr = { 0 };
Do we really need the initialization here? With the two if() conditions aligned we should be properly initializing it in all the cases we use it, or have I missed one?
thanks
-- PMM
Quoted 13 lines · Peter Maydell
Quoted 12 lines · Jens Freimann
This fixes coverity issue CID1005339.
Make sure that saddr is not used uninitialized if the mcast parameter is NULL.
Reported-by Peter Maydell <[email protected]>Signed-off-by Jens Freimann <[email protected]>--- net/socket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 14 lines
net/socket.c static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, · line 335
Quoted 10 lines
net/socket.c +1
-1@@ -332,7 +332,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,const char *mcast, Error **errp) { - struct sockaddr_in saddr; + struct sockaddr_in saddr = { 0 };Do we really need the initialization here? With the two
if()conditions aligned we should be properly initializing it in all the cases we use it, or have I missed one?
We don't need it. I added it not to have the same problem again if the code changes in the future. I think it shouldn't hurt because this code is only run once during initialization. If you think it's not necessary I'm fine with removing it though.
regards,
Jens
Quoted 2 lines
thanks
-- PMM
Quoted 9 lines · Jens Freimann
Quoted 3 lines · Peter Maydell
Do we really need the initialization here? With the two
if()conditions aligned we should be properly initializing it in all the cases we use it, or have I missed one?We don't need it. I added it not to have the same problem again if the code changes in the future. I think it shouldn't hurt because this code is only run once during initialization.
The idea is that we want the compiler to tell us if we use this state when it hasn't been properly initialized. Zeroing the variable means that the compiler won't warn, but we'll use zero data, which is unlikely to be the right thing. Occasionally we have to resort to zeroing variables if the compiler can't figure out that we always initialize it if we use it (older gcc versions sometimes produce spurious maybe-used-uninitialized warnings), but we only do that when we have to.
thanks
-- PMM
Hi Jan,
Quoted 17 lines · Jens Freimann
This fixes coverity issue CID1005339.
Make sure that saddr is not used uninitialized if the mcast parameter is NULL.
Reported-by Peter Maydell <[email protected]>Signed-off-by Jens Freimann <[email protected]>---
net/socket.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)net/socket.c +0
-0
net/socket.c static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, · line 335
Quoted 5 lines
net/socket.c +1
-1@@ -332,7 +332,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,const char *mcast, Error **errp) { - struct sockaddr_in saddr; + struct sockaddr_in saddr = { 0 };
Based on:
https://lists.gnu.org/archive/html/qemu-devel/2017-10/msg04946.html
It would appear that the use of {} to initialize the struct is preferred over {0}.
Thanks,
Darren.
Quoted 16 lines
net/socket.c +1
-1int newfd;NetClientState *nc;NetSocketState *s;@@ -373,7 +373,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,net_socket_read_poll (s, true);/* mcast: save bound address as dst */ -if (is_connected) { +if (is_connected && mcast !=NULL ) { s->dgram_dst = saddr; snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d (cloned mcast=%s:%d)",Signature · 1 line
2.13.6
Quoted 28 lines · Darren Kenny
Hi Jan,
Quoted 17 lines · Jens Freimann
This fixes coverity issue CID1005339.
Make sure that saddr is not used uninitialized if the mcast parameter is NULL.
Reported-by Peter Maydell <[email protected]>Signed-off-by Jens Freimann <[email protected]>---
net/socket.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)net/socket.c +0
-0net/socket.c static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, · line 335
Quoted 5 lines
net/socket.c +1
-1@@ -332,7 +332,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,const char *mcast, Error **errp) { - struct sockaddr_in saddr; + struct sockaddr_in saddr = { 0 };Based on:
Quoted 4 lines
https://lists.gnu.org/archive/html/qemu-devel/2017-10/msg04946.html
It would appear that the use of {} to initialize the struct is preferred over {0}.
Thanks for the hint Darren. I'll change it in the next version (or get rid of the initalization completely).
regards,
Jens
Jason Wang
AppliedQuoted 26 lines · Jens Freimann
This fixes coverity issue CID1005339.
Make sure that saddr is not used uninitialized if the mcast parameter is NULL.
Reported-by Peter Maydell <[email protected]>Signed-off-by Jens Freimann <[email protected]>--- net/socket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)net/socket.c +1
-1@@ -332,7 +332,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,const char *mcast, Error **errp) { - struct sockaddr_in saddr; + struct sockaddr_in saddr = { 0 };int newfd;NetClientState *nc;NetSocketState *s;
net/socket.c static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, · line 379
Quoted 8 lines
net/socket.c +1
-1@@ -373,7 +373,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,net_socket_read_poll (s, true);/* mcast: save bound address as dst */ -if (is_connected) { +if (is_connected && mcast !=NULL ) { s->dgram_dst = saddr; snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d (cloned mcast=%s:%d)",
Applied, thanks.
Quoted 34 lines · Jason Wang
Quoted 31 lines · Jens Freimann
This fixes coverity issue CID1005339.
Make sure that saddr is not used uninitialized if the mcast parameter is NULL.
Reported-by Peter Maydell <[email protected]>Signed-off-by Jens Freimann <[email protected]>--- net/socket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)net/socket.c +1
-1@@ -332,7 +332,7 @@ static NetSocketState *net_socket_fd_init_dgram (NetClientState *peer, const char *mcast, Error **errp) { -struct sockaddr_in saddr; +struct sockaddr_in saddr = {0 };int newfd;NetClientState *nc;NetSocketState *s;net/socket.c +0
-0@@ -373,7 +373,7 @@ static NetSocketState *net_socket_fd_init_dgram (NetClientState *peer, net_socket_read_poll(s, true);/* mcast: save bound address as dst */
Quoted 8 lines
net/socket.c static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, · line 379
Quoted 5 lines
net/socket.c +1
-1-if (is_connected) { +if (is_connected && mcast !=NULL ) { s->dgram_dst = saddr; snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d (cloned mcast=%s:%d)",Applied, thanks.
Er, this version didn't pass code review and you should apply v2 instead...
thanks
-- PMM
Quoted 40 lines · Peter Maydell
Quoted 31 lines · Jason Wang
Quoted 29 lines · Jens Freimann
This fixes coverity issue CID1005339.
Make sure that saddr is not used uninitialized if the mcast parameter is NULL.
Reported-by Peter Maydell <[email protected]>Signed-off-by Jens Freimann <[email protected]>--- net/socket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)net/socket.c +1
-1@@ -332,7 +332,7 @@ static NetSocketState *net_socket_fd_init_dgram (NetClientState *peer, const char *mcast, Error **errp) { -struct sockaddr_in saddr; +struct sockaddr_in saddr = {0 };int newfd;NetClientState *nc;NetSocketState *s;net/socket.c +0
-0@@ -373,7 +373,7 @@ static NetSocketState *net_socket_fd_init_dgram (NetClientState *peer, net_socket_read_poll(s, true);Quoted 8 lines
net/socket.c static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, · line 379
Quoted 6 lines
net/socket.c +1
-1/* mcast: save bound address as dst */ -if (is_connected) { +if (is_connected && mcast !=NULL ) { s->dgram_dst = saddr; snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d (cloned mcast=%s:%d)",Applied, thanks.
Quoted 5 lines
Er, this version didn't pass code review and you should apply v2 instead...
thanks
-- PMM
Oops, indeed.
Apply V2.
Thanks