Blame SOURCES/0008-device-info-remove-g_list_length-on-compare_addresse.patch

0985ad
From 66935d134e1f359eda5cfac053b0bf716811670a Mon Sep 17 00:00:00 2001
0985ad
From: Victor Toso <me@victortoso.com>
0985ad
Date: Fri, 12 Jul 2019 11:12:36 +0200
0985ad
Subject: [PATCH 8/9] device-info: remove g_list_length() on
0985ad
 compare_addresses()
0985ad
0985ad
The g_list_length() function does iterate over both lists to compare
0985ad
its length. Considering that we use this to check for true/false and
0985ad
we will iterate again on both lists, we can do so once.
0985ad
0985ad
This also avoids covscan/clang warnings:
0985ad
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:216:27: warning: Access to field 'data' results in a dereference of a null pointer (loaded from variable 'lb')
0985ad
 | #        PciDevice *devb = lb->data;
0985ad
 | #                          ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:397:5: note: Taking false branch
0985ad
 | #    if (!user_pci_addr) {
0985ad
 | #    ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:407:22: note: Calling 'find_device_at_pci_address'
0985ad
 | #    char *dev_path = find_device_at_pci_address(user_pci_addr, &vendor_id, &device_id);
0985ad
 | #                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:329:5: note: Taking true branch
0985ad
 | #    g_return_val_if_fail(pci_addr != NULL, NULL);
0985ad
 | #    ^
0985ad
 | /usr/include/glib-2.0/glib/gmessages.h:594:9: note: expanded from macro 'g_return_val_if_fail'
0985ad
 | #     if G_LIKELY(expr) { } else                                         \
0985ad
 | #        ^
0985ad
 | /usr/include/glib-2.0/glib/gmacros.h:385:43: note: expanded from macro 'G_LIKELY'
0985ad
 | ##define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR((expr)), 1))
0985ad
 | #                                          ^
0985ad
 | /usr/include/glib-2.0/glib/gmacros.h:379:4: note: expanded from macro '_G_BOOLEAN_EXPR'
0985ad
 | #   if (expr)                                    \
0985ad
 | #   ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:329:5: note: Taking true branch
0985ad
 | /usr/include/glib-2.0/glib/gmessages.h:594:6: note: expanded from macro 'g_return_val_if_fail'
0985ad
 | #     if G_LIKELY(expr) { } else                                         \
0985ad
 | #     ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:329:5: note: Loop condition is false.  Exiting loop
0985ad
 | /usr/include/glib-2.0/glib/gmessages.h:593:40: note: expanded from macro 'g_return_val_if_fail'
0985ad
 | ##define g_return_val_if_fail(expr,val)  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/vdagent/device-info.c:330:5: note: Taking true branch
0985ad
 | #    g_return_val_if_fail(device_id != NULL, NULL);
0985ad
 | #    ^
0985ad
 | /usr/include/glib-2.0/glib/gmessages.h:594:9: note: expanded from macro 'g_return_val_if_fail'
0985ad
 | #     if G_LIKELY(expr) { } else                                         \
0985ad
 | #        ^
0985ad
 | /usr/include/glib-2.0/glib/gmacros.h:385:43: note: expanded from macro 'G_LIKELY'
0985ad
 | ##define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR((expr)), 1))
0985ad
 | #                                          ^
0985ad
 | /usr/include/glib-2.0/glib/gmacros.h:379:4: note: expanded from macro '_G_BOOLEAN_EXPR'
0985ad
 | #   if (expr)                                    \
0985ad
 | #   ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:330:5: note: Taking true branch
0985ad
 | /usr/include/glib-2.0/glib/gmessages.h:594:6: note: expanded from macro 'g_return_val_if_fail'
0985ad
 | #     if G_LIKELY(expr) { } else                                         \
0985ad
 | #     ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:330:5: note: Loop condition is false.  Exiting loop
0985ad
 | /usr/include/glib-2.0/glib/gmessages.h:593:40: note: expanded from macro 'g_return_val_if_fail'
0985ad
 | ##define g_return_val_if_fail(expr,val)  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/vdagent/device-info.c:331:5: note: Taking true branch
0985ad
 | #    g_return_val_if_fail(vendor_id != NULL, NULL);
0985ad
 | #    ^
0985ad
 | /usr/include/glib-2.0/glib/gmessages.h:594:9: note: expanded from macro 'g_return_val_if_fail'
0985ad
 | #     if G_LIKELY(expr) { } else                                         \
0985ad
 | #        ^
0985ad
 | /usr/include/glib-2.0/glib/gmacros.h:385:43: note: expanded from macro 'G_LIKELY'
0985ad
 | ##define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR((expr)), 1))
0985ad
 | #                                          ^
0985ad
 | /usr/include/glib-2.0/glib/gmacros.h:379:4: note: expanded from macro '_G_BOOLEAN_EXPR'
0985ad
 | #   if (expr)                                    \
0985ad
 | #   ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:331:5: note: Taking true branch
0985ad
 | /usr/include/glib-2.0/glib/gmessages.h:594:6: note: expanded from macro 'g_return_val_if_fail'
0985ad
 | #     if G_LIKELY(expr) { } else                                         \
0985ad
 | #     ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:331:5: note: Loop condition is false.  Exiting loop
0985ad
 | /usr/include/glib-2.0/glib/gmessages.h:593:40: note: expanded from macro 'g_return_val_if_fail'
0985ad
 | ##define g_return_val_if_fail(expr,val)  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/vdagent/device-info.c:334:5: note: Loop condition is true.  Entering loop body
0985ad
 | #    for (int i = 0; i < 10; ++i) {
0985ad
 | #    ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:340:13: note: Assuming the condition is false
0985ad
 | #        if (stat(dev_path, &buf) != 0) {
0985ad
 | #            ^~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:340:9: note: Taking false branch
0985ad
 | #        if (stat(dev_path, &buf) != 0) {
0985ad
 | #        ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:355:13: note: Assuming the condition is false
0985ad
 | #        if (realpath(sys_path, device_link) == NULL) {
0985ad
 | #            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:355:9: note: Taking false branch
0985ad
 | #        if (realpath(sys_path, device_link) == NULL) {
0985ad
 | #        ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:361:36: note: Calling 'parse_pci_address_from_sysfs_path'
0985ad
 | #        PciAddress *drm_pci_addr = parse_pci_address_from_sysfs_path(device_link);
0985ad
 | #                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:129:9: note: Assuming 'pos' is non-null
0985ad
 | #    if (!pos) {
0985ad
 | #        ^~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:129:5: note: Taking false branch
0985ad
 | #    if (!pos) {
0985ad
 | #    ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:136:9: note: Assuming 'pos' is non-null
0985ad
 | #    if (!pos) {
0985ad
 | #        ^~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:136:5: note: Taking false branch
0985ad
 | #    if (!pos) {
0985ad
 | #    ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:142:9: note: Assuming 'pos' is non-null
0985ad
 | #    if (!pos) {
0985ad
 | #        ^~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:142:5: note: Taking false branch
0985ad
 | #    if (!pos) {
0985ad
 | #    ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:146:27: note: Calling 'pci_address_new'
0985ad
 | #    PciAddress *address = pci_address_new();
0985ad
 | #                          ^~~~~~~~~~~~~~~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:61:12: note: Taking false branch
0985ad
 | #    return g_new0(PciAddress, 1);
0985ad
 | #           ^
0985ad
 | /usr/include/glib-2.0/glib/gmem.h:279:42: note: expanded from macro 'g_new0'
0985ad
 | ##define g_new0(struct_type, n_structs)                  _G_NEW (struct_type, n_structs, malloc0)
0985ad
 | #                                                        ^
0985ad
 | /usr/include/glib-2.0/glib/gmem.h:211:4: note: expanded from macro '_G_NEW'
0985ad
 | #          if (__s == 1)                                         \
0985ad
 | #          ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:61:12: note: Left side of '&&' is false
0985ad
 | /usr/include/glib-2.0/glib/gmem.h:279:42: note: expanded from macro 'g_new0'
0985ad
 | ##define g_new0(struct_type, n_structs)                  _G_NEW (struct_type, n_structs, malloc0)
0985ad
 | #                                                        ^
0985ad
 | /usr/include/glib-2.0/glib/gmem.h:213:40: note: expanded from macro '_G_NEW'
0985ad
 | #          else if (__builtin_constant_p (__n) &&                \
0985ad
 | #                                              ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:61:12: note: Null pointer value stored to field 'devices'
0985ad
 | #    return g_new0(PciAddress, 1);
0985ad
 | #           ^~~~~~~~~~~~~~~~~~~~~
0985ad
 | /usr/include/glib-2.0/glib/gmem.h:279:42: note: expanded from macro 'g_new0'
0985ad
 | ##define g_new0(struct_type, n_structs)                  _G_NEW (struct_type, n_structs, malloc0)
0985ad
 | #                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 | /usr/include/glib-2.0/glib/gmem.h:217:12: note: expanded from macro '_G_NEW'
0985ad
 | #            __p = g_##func##_n (__n, __s);                      \
0985ad
 | #                  ^~~~~~~~~~~~~~~~~~~~~~~
0985ad
 | <scratch space>:76:1: note: expanded from here
0985ad
 | #g_malloc0_n
0985ad
 | #^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:146:27: note: Returning from 'pci_address_new'
0985ad
 | #    PciAddress *address = pci_address_new();
0985ad
 | #                          ^~~~~~~~~~~~~~~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:149:5: note: Loop condition is true.  Entering loop body
0985ad
 | #    while (pos) {
0985ad
 | #    ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:150:26: note: Taking false branch
0985ad
 | #        PciDevice *dev = g_new0(PciDevice, 1);
0985ad
 | #                         ^
0985ad
 | /usr/include/glib-2.0/glib/gmem.h:279:42: note: expanded from macro 'g_new0'
0985ad
 | ##define g_new0(struct_type, n_structs)                  _G_NEW (struct_type, n_structs, malloc0)
0985ad
 | #                                                        ^
0985ad
 | /usr/include/glib-2.0/glib/gmem.h:211:4: note: expanded from macro '_G_NEW'
0985ad
 | #          if (__s == 1)                                         \
0985ad
 | #          ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:150:26: note: Left side of '&&' is false
0985ad
 | /usr/include/glib-2.0/glib/gmem.h:279:42: note: expanded from macro 'g_new0'
0985ad
 | ##define g_new0(struct_type, n_structs)                  _G_NEW (struct_type, n_structs, malloc0)
0985ad
 | #                                                        ^
0985ad
 | /usr/include/glib-2.0/glib/gmem.h:213:40: note: expanded from macro '_G_NEW'
0985ad
 | #          else if (__builtin_constant_p (__n) &&                \
0985ad
 | #                                              ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:152:9: note: Taking true branch
0985ad
 | #        if (!parse_pci_device(pos + 1, next, dev)) {
0985ad
 | #        ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:154:13: note:  Execution continues on line 159
0985ad
 | #            break;
0985ad
 | #            ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:361:36: note: Returning from 'parse_pci_address_from_sysfs_path'
0985ad
 | #        PciAddress *drm_pci_addr = parse_pci_address_from_sysfs_path(device_link);
0985ad
 | #                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:362:9: note: Taking false branch
0985ad
 | #        if (!drm_pci_addr) {
0985ad
 | #        ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:367:14: note: Calling 'compare_addresses'
0985ad
 | #        if (!compare_addresses(pci_addr, drm_pci_addr)) {
0985ad
 | #             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:207:11: note: Assuming the condition is true
0985ad
 | #    if (!(a->domain == b->domain
0985ad
 | #          ^~~~~~~~~~~~~~~~~~~~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:207:11: note: Left side of '&&' is true
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:208:12: note: Assuming the condition is true
0985ad
 | #        && g_list_length(a->devices) == g_list_length(b->devices))) {
0985ad
 | #           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:207:5: note: Taking false branch
0985ad
 | #    if (!(a->domain == b->domain
0985ad
 | #    ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:212:35: note: 'lb' initialized to a null pointer value
0985ad
 | #    for (GList *la = a->devices, *lb = b->devices;
0985ad
 | #                                  ^~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:213:10: note: Assuming 'la' is not equal to NULL
0985ad
 | #         la != NULL;
0985ad
 | #         ^~~~~~~~~~
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:212:5: note: Loop condition is true.  Entering loop body
0985ad
 | #    for (GList *la = a->devices, *lb = b->devices;
0985ad
 | #    ^
0985ad
 | spice-vdagent-0.19.0/src/vdagent/device-info.c:216:27: note: Access to field 'data' results in a dereference of a null pointer (loaded from variable 'lb')
0985ad
 | #        PciDevice *devb = lb->data;
0985ad
 | #                          ^~
0985ad
 | #  214|            la = la->next, lb = lb->next) {
0985ad
 | #  215|           PciDevice *deva = la->data;
0985ad
 | #  216|->         PciDevice *devb = lb->data;
0985ad
 | #  217|
0985ad
 | #  218|           if (deva->slot != devb->slot
0985ad
0985ad
Signed-off-by: Victor Toso <victortoso@redhat.com>
0985ad
Acked-by: Frediano Ziglio <fziglio@redhat.com>
0985ad
---
0985ad
 src/vdagent/device-info.c | 12 +++++++-----
0985ad
 1 file changed, 7 insertions(+), 5 deletions(-)
0985ad
0985ad
diff --git a/src/vdagent/device-info.c b/src/vdagent/device-info.c
0985ad
index 4983543..6b0e28f 100644
0985ad
--- a/src/vdagent/device-info.c
0985ad
+++ b/src/vdagent/device-info.c
0985ad
@@ -204,13 +204,13 @@ static PciAddress* parse_pci_address_from_spice(char *input)
0985ad
 static bool compare_addresses(PciAddress *a, PciAddress *b)
0985ad
 {
0985ad
     // only check domain, slot, and function
0985ad
-    if (!(a->domain == b->domain
0985ad
-        && g_list_length(a->devices) == g_list_length(b->devices))) {
0985ad
+    if (a->domain != b->domain) {
0985ad
         return false;
0985ad
     }
0985ad
 
0985ad
-    for (GList *la = a->devices, *lb = b->devices;
0985ad
-         la != NULL;
0985ad
+    const GList *la, *lb;
0985ad
+    for (la = a->devices, lb = b->devices;
0985ad
+         la != NULL && lb != NULL;
0985ad
          la = la->next, lb = lb->next) {
0985ad
         PciDevice *deva = la->data;
0985ad
         PciDevice *devb = lb->data;
0985ad
@@ -220,7 +220,9 @@ static bool compare_addresses(PciAddress *a, PciAddress *b)
0985ad
             return false;
0985ad
         }
0985ad
     }
0985ad
-    return true;
0985ad
+
0985ad
+    /* True only if both have the same length */
0985ad
+    return (la == NULL && lb == NULL);
0985ad
 }
0985ad
 
0985ad
 // Connector type names from xorg modesetting driver
0985ad
-- 
0985ad
2.21.0
0985ad