Skip to contentSkip to conversation

tap: fix memory leak on failure in net_init_tap()

PATCH Applied Peter Maydell 2 participants 2 messages Jan 10, 2017 – Jan 11, 2017 Networking

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tap: fix memory leak on failure in net_init_tap()

Commit 091a6b2ac fixed most of the memory leaks in failure paths in net_init_tap() reported by Coverity (CID 1356216), but missed one. Fix it by deferring the allocation of fds and vhost_fds until after the error check.

---
 net/tap.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/tap.c +5 -2
index b6896a7..6248e85 100644
@@ -788,8 +788,8 @@ int net_init_tap(const Netdev *netdev, const char *name,
             return -1;
         }
     } else if (tap->has_fds) {
-        char **fds = g_new0(char *, MAX_TAP_QUEUES);
-        char **vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
+        char **fds;
+        char **vhost_fds;
         int nfds, nvhosts;
 
         if (tap->has_ifname || tap->has_script || tap->has_downscript ||
@@ -801,6 +801,9 @@ int net_init_tap(const Netdev *netdev, const char *name,
             return -1;
         }
 
+        fds = g_new0(char *, MAX_TAP_QUEUES);
+        vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
+
         nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
         if (tap->has_vhostfds) {
             nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
Signature · 1 line
2.7.4
Applied
#1 lore raw
Quoted 26 lines · Peter Maydell

Commit 091a6b2ac fixed most of the memory leaks in failure paths in net_init_tap() reported by Coverity (CID 1356216), but missed one. Fix it by deferring the allocation of fds and vhost_fds until after the error check.

---
  net/tap.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
net/tap.c +2 -2
index b6896a7..6248e85 100644
@@ -788,8 +788,8 @@ int net_init_tap(const Netdev *netdev, const char *name,
              return -1;
          }
      } else if (tap->has_fds) {
-        char **fds = g_new0(char *, MAX_TAP_QUEUES);
-        char **vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
+        char **fds;
+        char **vhost_fds;
          int nfds, nvhosts;
 
          if (tap->has_ifname || tap->has_script || tap->has_downscript ||

net/tap.c int net_init_tap(const Netdev *netdev, const char *name, · line 809

Quoted 9 lines
net/tap.c +3 -0
@@ -801,6 +801,9 @@ int net_init_tap(const Netdev *netdev, const char *name,
              return -1;
          }
 
+        fds = g_new0(char *, MAX_TAP_QUEUES);
+        vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
+
          nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
          if (tap->has_vhostfds) {
              nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);

Applied to -net.

Thanks