net: Fix various Coverity issues in vhost-user and passt
cover net: Fix various Coverity issues in vhost-user and passt
This patch series addresses several static analysis warnings reported by Coverity (CIDs 1612365, 1612368, 1612369, 1612371, 1612372, 1612375) in the vhost-user and passt networking code.
The changes are mostly minor cleanups and fixes:
Three patches remove an unused "err" variable and its associated unreachable error handling block.
One patch removes another piece of dead code in an error path by refactoring away a goto.
The remaining two patches initialize a pointer to prevent a potential illegal memory access and add a check for the return value of g_remove() to log potential errors.
Laurent Vivier (6):
net/passt: Remove unused "err" from passt_vhost_user_event() (CID
1612375)
net/vhost-user: Remove unused "err" from net_vhost_user_event() (CID
1612372)
net/passt: Remove dead code in passt_vhost_user_start error path (CID
1612371)
net/passt: Check return value of g_remove() in net_passt_cleanup()
(CID 1612369)
net/passt: Initialize "error" variable in net_passt_send() (CID
1612368) net/vhost-user: Remove unused "err" from chr_closed_bh() (CID 1612365)
net/passt.c | 22 +++++++--------------- net/vhost-user.c | 9 --------- 2 files changed, 7 insertions(+), 24 deletions(-)
Signature · 1 line
2.49.0
PATCH 1/6 net/passt: Remove unused "err" from passt_vhost_user_event() (CID 1612375)
The "err" variable was declared but never used within the passt_vhost_user_event() function. This resulted in a dead code warning (CID 1612375) from Coverity.
Remove the unused variable and the associated error block to resolve the issue.
--- net/passt.c | 5 ----- 1 file changed, 5 deletions(-)
net/passt.c +0 -5
@@ -397,7 +397,6 @@ err:static void passt_vhost_user_event (void *opaque,QEMUChrEvent event) {NetPasstState *s = opaque; -Error *err =NULL ;switch (event) {case CHR_EVENT_OPENED :
@@ -428,10 +427,6 @@ static void passt_vhost_user_event(void *opaque, QEMUChrEvent event)/* Ignore */ break ; } - -if (err) { -error_report_err (err); - } }static int net_passt_vhost_user_init (NetPasstState *s,Error **errp)
Signature · 1 line
2.49.0
Re: [PATCH 1/6] net/passt: Remove unused "err" from passt_vhost_user_event() (CID 1612375)
Quoted 10 lines · Laurent Vivier
The "err" variable was declared but never used within the
passt_vhost_user_event()function. This resulted in a dead code warning (CID 1612375) from Coverity.Remove the unused variable and the associated error block to resolve the issue.
Signed-off-by Laurent Vivier <[email protected]>---
thanks
-- PMM
PATCH 2/6 net/vhost-user: Remove unused "err" from net_vhost_user_event() (CID 1612372)
In reply to Laurent VivierThe "err" variable was declared but never used within the net_vhost_user_event() function. This resulted in a dead code warning (CID 1612372) from Coverity.
Remove the unused variable and the associated error block to resolve the issue.
--- net/vhost-user.c | 5 ----- 1 file changed, 5 deletions(-)
net/vhost-user.c +0 -5
@@ -329,7 +329,6 @@ static void net_vhost_user_event(void *opaque, QEMUChrEvent event)NetClientState *ncs[MAX_QUEUE_NUM ];NetVhostUserState *s;Chardev *chr; -Error *err =NULL ;int queues; queues = qemu_find_net_clients_except(name, ncs,
@@ -375,10 +374,6 @@ static void net_vhost_user_event(void *opaque, QEMUChrEvent event)/* Ignore */ break ; } - -if (err) { -error_report_err (err); - } } static int net_vhost_user_init(NetClientState *peer, const char *device,
Signature · 1 line
2.49.0
Re: [PATCH 2/6] net/vhost-user: Remove unused "err" from net_vhost_user_event() (CID 1612372)
Quoted 26 lines · Laurent Vivier
The "err" variable was declared but never used within the
net_vhost_user_event()function. This resulted in a dead code warning (CID 1612372) from Coverity.Remove the unused variable and the associated error block to resolve the issue.
Signed-off-by Laurent Vivier <[email protected]>--- net/vhost-user.c | 5 ----- 1 file changed, 5 deletions(-)net/vhost-user.c +0
-1@@ -329,7 +329,6 @@ static void net_vhost_user_event(void *opaque, QEMUChrEvent event)NetClientState *ncs[MAX_QUEUE_NUM ];NetVhostUserState *s;Chardev *chr; -Error *err =NULL ;int queues; queues = qemu_find_net_clients_except(name, ncs,
net/vhost-user.c static void net_vhost_user_event(void *opaque, QEMUChrEvent event) · line 377
Quoted 8 lines
net/vhost-user.c +0
-4@@ -375,10 +374,6 @@ static void net_vhost_user_event(void *opaque, QEMUChrEvent event)/* Ignore */ break ; } - -if (err) { -error_report_err (err); - }
thanks
-- PMM
PATCH 3/6 net/passt: Remove dead code in passt_vhost_user_start error path (CID 1612371)
In reply to Laurent VivierIn passt_vhost_user_start(), if vhost_net_init() fails, the "net" variable is NULL and execution jumps to the "err:" label.
The cleanup code within this label is conditioned on "if (net)", which can never be true in this error case. This makes the cleanup block dead code, as reported by Coverity (CID 1612371).
Refactor the error handling to occur inline, removing the goto and the unreachable cleanup block.
--- net/passt.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
net/passt.c +2 -8
@@ -375,7 +375,8 @@ static int passt_vhost_user_start(NetPasstState *s, VhostUserState *be) net =vhost_net_init (&options);if (!net) {error_report ("failed to init passt vhost_net" ); - goto err; +passt_vhost_user_stop (s); +return -1 ; }if (s->vhost_net) {
@@ -385,13 +386,6 @@ static int passt_vhost_user_start(NetPasstState *s, VhostUserState *be) s->vhost_net = net;return 0 ; -err: -if (net) { -vhost_net_cleanup (net); -g_free (net); - } -passt_vhost_user_stop (s); -return -1 ; }static void passt_vhost_user_event (void *opaque,QEMUChrEvent event)
Signature · 1 line
2.49.0
Re: [PATCH 3/6] net/passt: Remove dead code in passt_vhost_user_start error path (CID 1612371)
Quoted 11 lines · Laurent Vivier
In
passt_vhost_user_start(), ifvhost_net_init()fails, the "net" variable is NULL and execution jumps to the "err:" label.The cleanup code within this label is conditioned on "if (net)", which can never be true in this error case. This makes the cleanup block dead code, as reported by Coverity (CID 1612371).
Refactor the error handling to occur inline, removing the goto and the unreachable cleanup block.
thanks
-- PMM
PATCH 4/6 net/passt: Check return value of g_remove() in net_passt_cleanup() (CID 1612369)
In reply to Laurent VivierIf g_remove() fails, use warn_report() to log an error.
--- net/passt.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/passt.c +4 -1
@@ -103,7 +103,10 @@ static void net_passt_cleanup(NetClientState *nc)#endif kill (s->pid,SIGTERM ); -g_remove (s->pidfile); +if (g_remove (s->pidfile) !=0 ) { +warn_report ("Failed to remove passt pidfile %s: %s" , + s->pidfile,strerror (errno)); + }g_free (s->pidfile);g_ptr_array_free (s->args, TRUE); }
Signature · 1 line
2.49.0
Re: [PATCH 4/6] net/passt: Check return value of g_remove() in net_passt_cleanup() (CID 1612369)
Quoted 8 lines · Laurent Vivier
If
g_remove()fails, usewarn_report()to log an error.Signed-off-by Laurent Vivier <[email protected]>--- net/passt.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
thanks
-- PMM
PATCH 5/6 net/passt: Initialize "error" variable in net_passt_send() (CID 1612368)
In reply to Laurent VivierThis was flagged by Coverity as a memory illegal access.
Initialize the pointer to NULL at declaration.
--- net/passt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/passt.c +1 -1
@@ -124,7 +124,7 @@ static gboolean net_passt_send(QIOChannel *ioc, GIOCondition condition, {if (net_stream_data_send (ioc, condition, data) ==G_SOURCE_REMOVE ) {NetPasstState *s =DO_UPCAST (NetPasstState, data, data); -Error *error; +Error *error =NULL ;/* we need to restart passt */ kill (s->pid,SIGTERM );
Signature · 1 line
2.49.0
Re: [PATCH 5/6] net/passt: Initialize "error" variable in net_passt_send() (CID 1612368)
Quoted 24 lines · Laurent Vivier
This was flagged by Coverity as a memory illegal access.
Initialize the pointer to NULL at declaration.
Signed-off-by Laurent Vivier <[email protected]>--- net/passt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/passt.c +1
-2@@ -124,7 +124,7 @@ static gboolean net_passt_send(QIOChannel *ioc, GIOCondition condition, {if (net_stream_data_send (ioc, condition, data) ==G_SOURCE_REMOVE ) {NetPasstState *s =DO_UPCAST (NetPasstState, data, data); -Error *error; +Error *error =NULL ;/* we need to restart passt */ kill (s->pid,SIGTERM ); --
Quoted 3 lines
2.49.0
thanks
-- PMM
PATCH 6/6 net/vhost-user: Remove unused "err" from chr_closed_bh() (CID 1612365)
In reply to Laurent VivierThe "err" variable was declared but never used within the chr_closed_bh() function. This resulted in a dead code warning (CID 1612365) from Coverity.
Remove the unused variable and the associated error block to resolve the issue.
--- net/vhost-user.c | 4 ---- 1 file changed, 4 deletions(-)
net/vhost-user.c +0 -4
@@ -298,7 +298,6 @@ static void chr_closed_bh(void *opaque)const char *name = opaque;NetClientState *ncs[MAX_QUEUE_NUM ];NetVhostUserState *s; -Error *err =NULL ;int queues, i; queues = qemu_find_net_clients_except(name, ncs,
@@ -317,9 +316,6 @@ static void chr_closed_bh(void *opaque)qemu_chr_fe_set_handlers (&s->chr,NULL ,NULL , net_vhost_user_event,NULL , opaque,NULL , true); -if (err) { -error_report_err (err); - }qapi_event_send_netdev_vhost_user_disconnected (name); }
Signature · 1 line
2.49.0
Re: [PATCH 6/6] net/vhost-user: Remove unused "err" from chr_closed_bh() (CID 1612365)
Quoted 26 lines · Laurent Vivier
The "err" variable was declared but never used within the
chr_closed_bh()function. This resulted in a dead code warning (CID 1612365) from Coverity.Remove the unused variable and the associated error block to resolve the issue.
Signed-off-by Laurent Vivier <[email protected]>--- net/vhost-user.c | 4 ---- 1 file changed, 4 deletions(-)net/vhost-user.c +0
-1@@ -298,7 +298,6 @@ static void chr_closed_bh(void *opaque)const char *name = opaque;NetClientState *ncs[MAX_QUEUE_NUM ];NetVhostUserState *s; -Error *err =NULL ;int queues, i; queues = qemu_find_net_clients_except(name, ncs,
net/vhost-user.c static void chr_closed_bh(void *opaque) · line 321
Quoted 9 lines
net/vhost-user.c +0
-3@@ -317,9 +316,6 @@ static void chr_closed_bh(void *opaque)qemu_chr_fe_set_handlers (&s->chr,NULL ,NULL , net_vhost_user_event,NULL , opaque,NULL , true); -if (err) { -error_report_err (err); - }qapi_event_send_netdev_vhost_user_disconnected (name); }
thanks
-- PMM