Skip to contentSkip to conversation

net/socket: fix coverity issue

PATCH Applied Jens Freimann 4 participants 9 messages Nov 6, 2017 – Nov 13, 2017

Subsystems (2)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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
index e6b471c63d..51eaea67a0 100644
@@ -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
#1 lore raw
Quoted 17 lines · Jens Freimann

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 +0 -0
index e6b471c63d..51eaea67a0 100644

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

#4 lore raw
In reply to Jens Freimann

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.

---
net/socket.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
net/socket.c +0 -0
index e6b471c63d..51eaea67a0 100644

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 -1
    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
#2 lore raw
In reply to Peter Maydell
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.

---
 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
index e6b471c63d..51eaea67a0 100644
@@ -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

#5 lore raw
In reply to Darren Kenny
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.

---
net/socket.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
net/socket.c +0 -0
index e6b471c63d..51eaea67a0 100644

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:

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

#3 lore raw
In reply to Jens Freimann
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

Applied
#6 lore raw
In reply to Jens Freimann
Quoted 26 lines · Jens Freimann

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 +1 -1
index e6b471c63d..51eaea67a0 100644
@@ -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.

#7 lore raw
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.

---
  net/socket.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
net/socket.c +1 -1
index e6b471c63d..51eaea67a0 100644
@@ -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

#8 lore raw
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.

---
   net/socket.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
net/socket.c +1 -1
index e6b471c63d..51eaea67a0 100644
@@ -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