Blame SOURCES/0011-covscan-avoid-false-positive-on-g_clear_pointer.patch

0985ad
From fb30924e6f5c4e98aaa2c4e4d95b00b237289cb3 Mon Sep 17 00:00:00 2001
0985ad
From: Victor Toso <me@victortoso.com>
0985ad
Date: Tue, 27 Aug 2019 10:59:30 +0200
0985ad
Subject: [PATCH 11/11] covscan: avoid false positive on g_clear_pointer()
0985ad
0985ad
This is a CLANG_WARNING found by covscan. It is a false positive as
0985ad
g_clear_pointer() does set vportp to NULL, meaning that the situation
0985ad
described by covscan below should not be reached. Moving away from
0985ad
g_clear_pointer() in this specific case just to make our tool happy.
0985ad
0985ad
Covscan report:
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:458:9: warning: Use of memory after it is freed
0985ad
 > #    if (wbuf->write_pos != wbuf->size) {
0985ad
 > #        ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:268:12: note: Assuming the condition is true
0985ad
 > #    while (*vportp && (*vportp)->write_buf)
0985ad
 > #           ^~~~~~~
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:268:12: note: Left side of '&&' is true
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:268:5: note: Loop condition is true.  Entering loop body
0985ad
 > #    while (*vportp && (*vportp)->write_buf)
0985ad
 > #    ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:269:9: note: Calling 'vdagent_virtio_port_do_write'
0985ad
 > #        vdagent_virtio_port_do_write(vportp);
0985ad
 > #        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:453:5: note: Taking false branch
0985ad
 > #    if (!wbuf) {
0985ad
 > #    ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:458:9: note: Assuming the condition is false
0985ad
 > #    if (wbuf->write_pos != wbuf->size) {
0985ad
 > #        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:458:5: note: Taking false branch
0985ad
 > #    if (wbuf->write_pos != wbuf->size) {
0985ad
 > #    ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:465:9: note: Assuming 'n' is < 0
0985ad
 > #    if (n < 0) {
0985ad
 > #        ^~~~~
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:465:5: note: Taking true branch
0985ad
 > #    if (n < 0) {
0985ad
 > #    ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:466:13: note: Assuming the condition is false
0985ad
 > #        if (errno == EINTR)
0985ad
 > #            ^~~~~~~~~~~~~~
0985ad
 > /usr/include/errno.h:38:16: note: expanded from macro 'errno'
0985ad
 > ## define errno (*__errno_location ())
0985ad
 > #               ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:466:9: note: Taking false branch
0985ad
 > #        if (errno == EINTR)
0985ad
 > #        ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:469:9: note: Calling 'vdagent_virtio_port_destroy'
0985ad
 > #        vdagent_virtio_port_destroy(vportp);
0985ad
 > #        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:130:5: note: Taking false branch
0985ad
 > #    if (!vport)
0985ad
 > #    ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:133:9: note: Assuming the condition is false
0985ad
 > #    if (vport->disconnect_callback)
0985ad
 > #        ^~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:133:5: note: Taking false branch
0985ad
 > #    if (vport->disconnect_callback)
0985ad
 > #    ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:137:5: note: Loop condition is true.  Entering loop body
0985ad
 > #    while (wbuf) {
0985ad
 > #    ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:140:9: note: Memory is released
0985ad
 > #        g_free(wbuf);
0985ad
 > #        ^~~~~~~~~~~~
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:137:5: note: Loop condition is false. Execution continues on line 144
0985ad
 > #    while (wbuf) {
0985ad
 > #    ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:144:5: note: Loop condition is true.  Entering loop body
0985ad
 > #    for (i = 0; i < VDP_END_PORT; i++) {
0985ad
 > #    ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:144:5: note: Loop condition is true.  Entering loop body
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:144:5: note: Loop condition is true.  Entering loop body
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:144:5: note: Loop condition is false. Execution continues on line 148
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:149:5: note: Assuming '_p' is null
0985ad
 > #    g_clear_pointer(vportp, g_free);
0985ad
 > #    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 > /usr/include/glib-2.0/glib/gmem.h:124:9: note: expanded from macro 'g_clear_pointer'
0985ad
 > #    if (_p)                                                                    \
0985ad
 > #        ^~
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:149:5: note: Taking false branch
0985ad
 > /usr/include/glib-2.0/glib/gmem.h:124:5: note: expanded from macro 'g_clear_pointer'
0985ad
 > #    if (_p)                                                                    \
0985ad
 > #    ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:149:5: note: Loop condition is false.  Exiting loop
0985ad
 > /usr/include/glib-2.0/glib/gmem.h:114:3: note: expanded from macro 'g_clear_pointer'
0985ad
 > #  G_STMT_START {                                                               \
0985ad
 > #  ^
0985ad
 > /usr/include/glib-2.0/glib/gmacros.h:346:23: note: expanded from macro 'G_STMT_START'
0985ad
 > ##define G_STMT_START  do
0985ad
 > #                      ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:469:9: note: Returning; memory was released
0985ad
 > #        vdagent_virtio_port_destroy(vportp);
0985ad
 > #        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:269:9: note: Returning; memory was released
0985ad
 > #        vdagent_virtio_port_do_write(vportp);
0985ad
 > #        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:268:12: note: Left side of '&&' is true
0985ad
 > #    while (*vportp && (*vportp)->write_buf)
0985ad
 > #           ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:268:5: note: Loop condition is true.  Entering loop body
0985ad
 > #    while (*vportp && (*vportp)->write_buf)
0985ad
 > #    ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:269:9: note: Calling 'vdagent_virtio_port_do_write'
0985ad
 > #        vdagent_virtio_port_do_write(vportp);
0985ad
 > #        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:453:5: note: Taking false branch
0985ad
 > #    if (!wbuf) {
0985ad
 > #    ^
0985ad
 > spice-vdagent-0.19.0/src/vdagentd/virtio-port.c:458:9: note: Use of memory after it is freed
0985ad
 > #    if (wbuf->write_pos != wbuf->size) {
0985ad
 > #        ^~~~~~~~~~~~~~~
0985ad
 > #  456|       }
0985ad
 > #  457|
0985ad
 > #  458|->     if (wbuf->write_pos != wbuf->size) {
0985ad
 > #  459|           syslog(LOG_ERR, "do_write: buffer is incomplete!!");
0985ad
 > #  460|           return;
0985ad
0985ad
Signed-off-by: Victor Toso <victortoso@redhat.com>
0985ad
---
0985ad
 src/vdagentd/virtio-port.c | 3 ++-
0985ad
 1 file changed, 2 insertions(+), 1 deletion(-)
0985ad
0985ad
diff --git a/src/vdagentd/virtio-port.c b/src/vdagentd/virtio-port.c
0985ad
index b0556ce..3ae7f22 100644
0985ad
--- a/src/vdagentd/virtio-port.c
0985ad
+++ b/src/vdagentd/virtio-port.c
0985ad
@@ -146,7 +146,8 @@ void vdagent_virtio_port_destroy(struct vdagent_virtio_port **vportp)
0985ad
     }
0985ad
 
0985ad
     close(vport->fd);
0985ad
-    g_clear_pointer(vportp, g_free);
0985ad
+    g_free(vport);
0985ad
+    *vportp = NULL;
0985ad
 }
0985ad
 
0985ad
 int vdagent_virtio_port_fill_fds(struct vdagent_virtio_port *vport,
0985ad
-- 
0985ad
2.21.0
0985ad