Skip to contentSkip to conversation

hw/nvme: use GPtrArray for blocker_features

PATCH In review Alexander Mikhalitsyn 3 participants 7 messages Jul 9 – Jul 28

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/nvme: use GPtrArray for blocker_features

From: Alexander Mikhalitsyn <[email protected]>

Let's use GPtrArray to build a list of blocker features and then g_strjoinv() to build a final comma-delimited string.

While previous approach was technically correct, it is fragile (because we need to take care of static buffer size choice) and Coverity dislikes it too.

Note, that we use g_ptr_array_new() to allocate array which means that GDestroyNotify callback is not set, so we can pass pointers to a static memory like g_ptr_array_add(..., (gpointer) "SR-IOV") without any problems as there won't be any attempt to free that memory.

Resolves: Coverity CID 1663673

---
 hw/nvme/ctrl.c | 41 +++++++++++++++++------------------------
 1 file changed, 17 insertions(+), 24 deletions(-)
hw/nvme/ctrl.c +17 -24
index a67e1598891..95a586d82a9 100644
@@ -9352,22 +9352,11 @@ static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev)
     }
 }
 
-#define BLOCKER_FEATURES_MAX_LEN 256
-
-static inline void nvme_add_blocker_feature(char *blocker_features,
-                                            const char *feature)
-{
-    if (strlen(blocker_features) > 0) {
-        g_strlcat(blocker_features, ", ", BLOCKER_FEATURES_MAX_LEN);
-    }
-    g_strlcat(blocker_features, feature, BLOCKER_FEATURES_MAX_LEN);
-}
-
 static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
                                         Error **errp)
 {
     uint64_t unsupported_cap, cap = ldq_le_p(&n->bar.cap);
-    char blocker_features[BLOCKER_FEATURES_MAX_LEN] = "";
+    g_autoptr(GPtrArray) blocker_features = g_ptr_array_new();
     bool adm_cmd_security_checked = false;
     bool cmd_io_mgmt_checked = false;
     bool cmd_zone_checked = false;
@@ -9416,15 +9405,15 @@ static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
             }
 
             if (namespaces_num > 1) {
-                nvme_add_blocker_feature(blocker_features,
-                                         "Namespace Attachment");
+                g_ptr_array_add(blocker_features,
+                                         (gpointer) "Namespace Attachment");
             }
 
             break;
         }
         case NVME_ADM_CMD_VIRT_MNGMT:
             if (n->params.sriov_max_vfs) {
-                nvme_add_blocker_feature(blocker_features, "SR-IOV");
+                g_ptr_array_add(blocker_features, (gpointer) "SR-IOV");
             }
 
             break;
@@ -9435,7 +9424,7 @@ static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
             }
 
             if (pci_dev->spdm_port) {
-                nvme_add_blocker_feature(blocker_features, "SPDM");
+                g_ptr_array_add(blocker_features, (gpointer) "SPDM");
             }
 
             adm_cmd_security_checked = true;
@@ -9469,7 +9458,7 @@ static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
 
             /* check for NVME_IOMS_MO_RUH_UPDATE */
             if (n->subsys->params.fdp.enabled) {
-                nvme_add_blocker_feature(blocker_features, "FDP");
+                g_ptr_array_add(blocker_features, (gpointer) "FDP");
             }
 
             cmd_io_mgmt_checked = true;
@@ -9504,8 +9493,8 @@ static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
                 }
 
                 if (ns->params.zoned) {
-                    nvme_add_blocker_feature(blocker_features,
-                                             "Zoned Namespace");
+                    g_ptr_array_add(blocker_features,
+                                             (gpointer) "Zoned Namespace");
                     break;
                 }
             }
@@ -9525,24 +9514,28 @@ static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
      * covered by unsupported_cap check.
      */
     if (NVME_CAP_CMBS(cap)) {
-        nvme_add_blocker_feature(blocker_features, "CMB");
+        g_ptr_array_add(blocker_features, (gpointer) "CMB");
         cap &= ~((uint64_t)CAP_CMBS_MASK << CAP_CMBS_SHIFT);
     }
 
     if (NVME_CAP_PMRS(cap)) {
-        nvme_add_blocker_feature(blocker_features, "PMR");
+        g_ptr_array_add(blocker_features, (gpointer) "PMR");
         cap &= ~((uint64_t)CAP_PMRS_MASK << CAP_PMRS_SHIFT);
     }
 
     unsupported_cap = cap & ~NVME_MIGRATION_SUPPORTED_CAP_BITS;
     if (unsupported_cap) {
-        nvme_add_blocker_feature(blocker_features, "unknown capability");
+        g_ptr_array_add(blocker_features, (gpointer) "unknown capability");
     }
 
     assert(n->migration_blocker == NULL);
-    if (strlen(blocker_features) > 0) {
+    if (blocker_features->len > 0) {
+        g_autofree char *blocker_list = NULL;
+
+        g_ptr_array_add(blocker_features, NULL);
+        blocker_list = g_strjoinv(", ", (void *)blocker_features->pdata);
         error_setg(&n->migration_blocker,
-                   "Migration is not supported for %s", blocker_features);
+                   "Migration is not supported for %s", blocker_list);
         if (migrate_add_blocker(&n->migration_blocker, errp) < 0) {
             return false;
         }
Signature · 1 line
2.47.3
#1 lore raw
Quoted 51 lines · Alexander Mikhalitsyn

From: Alexander Mikhalitsyn <[email protected]>

Let's use GPtrArray to build a list of blocker features and then g_strjoinv() to build a final comma-delimited string.

While previous approach was technically correct, it is fragile (because we need to take care of static buffer size choice) and Coverity dislikes it too.

Note, that we use g_ptr_array_new() to allocate array which means that GDestroyNotify callback is not set, so we can pass pointers to a static memory like g_ptr_array_add(..., (gpointer) "SR-IOV") without any problems as there won't be any attempt to free that memory.

Resolves: Coverity CID 1663673

---
 hw/nvme/ctrl.c | 41 +++++++++++++++++------------------------
 1 file changed, 17 insertions(+), 24 deletions(-)
hw/nvme/ctrl.c +1 -12
index a67e1598891..95a586d82a9 100644
@@ -9352,22 +9352,11 @@ static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev)
     }
 }
 
-#define BLOCKER_FEATURES_MAX_LEN 256
-
-static inline void nvme_add_blocker_feature(char *blocker_features,
-                                            const char *feature)
-{
-    if (strlen(blocker_features) > 0) {
-        g_strlcat(blocker_features, ", ", BLOCKER_FEATURES_MAX_LEN);
-    }
-    g_strlcat(blocker_features, feature, BLOCKER_FEATURES_MAX_LEN);
-}
-
 static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
                                         Error **errp)
 {
     uint64_t unsupported_cap, cap = ldq_le_p(&n->bar.cap);
-    char blocker_features[BLOCKER_FEATURES_MAX_LEN] = "";
+    g_autoptr(GPtrArray) blocker_features = g_ptr_array_new();
     bool adm_cmd_security_checked = false;
     bool cmd_io_mgmt_checked = false;
     bool cmd_zone_checked = false;

hw/nvme/ctrl.c static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev, · line 9409

Quoted 7 lines
hw/nvme/ctrl.c +2 -2
@@ -9416,15 +9405,15 @@ static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
             }
 
             if (namespaces_num > 1) {
-                nvme_add_blocker_feature(blocker_features,
-                                         "Namespace Attachment");
+                g_ptr_array_add(blocker_features,
+                                         (gpointer) "Namespace Attachment");

Is the cast here because we're dropping the const property of the literal string?

thanks
-- PMM

#2 lore raw

Am Do., 9. Juli 2026 um 10:51 Uhr schrieb Peter Maydell <[email protected]>:

Quoted 56 lines
Quoted 53 lines · Alexander Mikhalitsyn

From: Alexander Mikhalitsyn <[email protected]>

Let's use GPtrArray to build a list of blocker features and then g_strjoinv() to build a final comma-delimited string.

While previous approach was technically correct, it is fragile (because we need to take care of static buffer size choice) and Coverity dislikes it too.

Note, that we use g_ptr_array_new() to allocate array which means that GDestroyNotify callback is not set, so we can pass pointers to a static memory like g_ptr_array_add(..., (gpointer) "SR-IOV") without any problems as there won't be any attempt to free that memory.

Resolves: Coverity CID 1663673

---
 hw/nvme/ctrl.c | 41 +++++++++++++++++------------------------
 1 file changed, 17 insertions(+), 24 deletions(-)
hw/nvme/ctrl.c +1 -12
index a67e1598891..95a586d82a9 100644
@@ -9352,22 +9352,11 @@ static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev)
     }
 }
 
-#define BLOCKER_FEATURES_MAX_LEN 256
-
-static inline void nvme_add_blocker_feature(char *blocker_features,
-                                            const char *feature)
-{
-    if (strlen(blocker_features) > 0) {
-        g_strlcat(blocker_features, ", ", BLOCKER_FEATURES_MAX_LEN);
-    }
-    g_strlcat(blocker_features, feature, BLOCKER_FEATURES_MAX_LEN);
-}
-
 static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
                                         Error **errp)
 {
     uint64_t unsupported_cap, cap = ldq_le_p(&n->bar.cap);
-    char blocker_features[BLOCKER_FEATURES_MAX_LEN] = "";
+    g_autoptr(GPtrArray) blocker_features = g_ptr_array_new();
     bool adm_cmd_security_checked = false;
     bool cmd_io_mgmt_checked = false;
     bool cmd_zone_checked = false;
@@ -9416,15 +9405,15 @@ static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
             }
 
Quoted 6 lines

hw/nvme/ctrl.c static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev, · line 9409

Quoted 5 lines
hw/nvme/ctrl.c +2 -2
             if (namespaces_num > 1) {
-                nvme_add_blocker_feature(blocker_features,
-                                         "Namespace Attachment");
+                g_ptr_array_add(blocker_features,
+                                         (gpointer) "Namespace Attachment");

Hi Peter,

Quoted 2 lines

Is the cast here because we're dropping the const property of the literal string?

yep.

Compiler output without this cast:

Quoted 2 lines

../hw/nvme/ctrl.c:9418:42: error: passing 'const char[21]' to parameter of type 'gpointer' (aka 'void *') discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
9418 | "Namespace Attachment");

Kind regards,
Alex

Quoted 3 lines

thanks
-- PMM

Reviewed-by
#3 lore raw
Quoted 36 lines · Alexander Mikhalitsyn

Am Do., 9. Juli 2026 um 10:51 Uhr schrieb Peter Maydell <[email protected]>:

Quoted 23 lines
Quoted 19 lines · Alexander Mikhalitsyn

From: Alexander Mikhalitsyn <[email protected]>

Let's use GPtrArray to build a list of blocker features and then g_strjoinv() to build a final comma-delimited string.

While previous approach was technically correct, it is fragile (because we need to take care of static buffer size choice) and Coverity dislikes it too.

Note, that we use g_ptr_array_new() to allocate array which means that GDestroyNotify callback is not set, so we can pass pointers to a static memory like g_ptr_array_add(..., (gpointer) "SR-IOV") without any problems as there won't be any attempt to free that memory.

Resolves: Coverity CID 1663673

---

Quoted 9 lines

hw/nvme/ctrl.c static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev, · line 9409

Quoted 8 lines
hw/nvme/ctrl.c +2 -2
@@ -9416,15 +9405,15 @@ static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
             }
 
             if (namespaces_num > 1) {
-                nvme_add_blocker_feature(blocker_features,
-                                         "Namespace Attachment");
+                g_ptr_array_add(blocker_features,
+                                         (gpointer) "Namespace Attachment");
Quoted 6 lines

Hi Peter,

Quoted 2 lines

Is the cast here because we're dropping the const property of the literal string?

yep.

Mmm. Unfortunate but unavoidable I guess.

The glib docs recommend using "void *" instead of "gpointer" in new code: https://docs.gtk.org/glib/types.html#gpointer
but either way

-- PMM

#4 lore raw

Am Do., 9. Juli 2026 um 10:58 Uhr schrieb Peter Maydell <[email protected]>:

Quoted 44 lines
Quoted 41 lines · Alexander Mikhalitsyn

Am Do., 9. Juli 2026 um 10:51 Uhr schrieb Peter Maydell <[email protected]>:

Quoted 23 lines
Quoted 19 lines · Alexander Mikhalitsyn

From: Alexander Mikhalitsyn <[email protected]>

Let's use GPtrArray to build a list of blocker features and then g_strjoinv() to build a final comma-delimited string.

While previous approach was technically correct, it is fragile (because we need to take care of static buffer size choice) and Coverity dislikes it too.

Note, that we use g_ptr_array_new() to allocate array which means that GDestroyNotify callback is not set, so we can pass pointers to a static memory like g_ptr_array_add(..., (gpointer) "SR-IOV") without any problems as there won't be any attempt to free that memory.

Resolves: Coverity CID 1663673

---

Quoted 9 lines

hw/nvme/ctrl.c static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev, · line 9409

Quoted 8 lines
hw/nvme/ctrl.c +2 -2
@@ -9416,15 +9405,15 @@ static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
             }
 
             if (namespaces_num > 1) {
-                nvme_add_blocker_feature(blocker_features,
-                                         "Namespace Attachment");
+                g_ptr_array_add(blocker_features,
+                                         (gpointer) "Namespace Attachment");

Hi Peter,

Quoted 2 lines

Is the cast here because we're dropping the const property of the literal string?

Quoted 7 lines
Quoted 1 line

yep.

Mmm. Unfortunate but unavoidable I guess.

The glib docs recommend using "void *" instead of "gpointer" in new code: https://docs.gtk.org/glib/types.html#gpointer
but either way

ah, I didn't know that. My first intention was to use "void *", but then I searched over QEMU codebase
and found a few gpointer casts in exact use-case (with g_ptr_array_add).

I can correct this and resend if you want ;)

Quoted 4 lines

-- PMM

Reviewed-by
#5 lore raw
Quoted 54 lines · Alexander Mikhalitsyn

Am Do., 9. Juli 2026 um 10:58 Uhr schrieb Peter Maydell <[email protected]>:

Quoted 44 lines
Quoted 41 lines · Alexander Mikhalitsyn

Am Do., 9. Juli 2026 um 10:51 Uhr schrieb Peter Maydell <[email protected]>:

Quoted 23 lines
Quoted 19 lines · Alexander Mikhalitsyn

From: Alexander Mikhalitsyn <[email protected]>

Let's use GPtrArray to build a list of blocker features and then g_strjoinv() to build a final comma-delimited string.

While previous approach was technically correct, it is fragile (because we need to take care of static buffer size choice) and Coverity dislikes it too.

Note, that we use g_ptr_array_new() to allocate array which means that GDestroyNotify callback is not set, so we can pass pointers to a static memory like g_ptr_array_add(..., (gpointer) "SR-IOV") without any problems as there won't be any attempt to free that memory.

Resolves: Coverity CID 1663673

---

Quoted 9 lines

hw/nvme/ctrl.c static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev, · line 9409

Quoted 8 lines
hw/nvme/ctrl.c +2 -2
@@ -9416,15 +9405,15 @@ static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
             }
 
             if (namespaces_num > 1) {
-                nvme_add_blocker_feature(blocker_features,
-                                         "Namespace Attachment");
+                g_ptr_array_add(blocker_features,
+                                         (gpointer) "Namespace Attachment");

Hi Peter,

Quoted 2 lines

Is the cast here because we're dropping the const property of the literal string?

Quoted 7 lines
Quoted 1 line

yep.

Mmm. Unfortunate but unavoidable I guess.

The glib docs recommend using "void *" instead of "gpointer" in new code: https://docs.gtk.org/glib/types.html#gpointer
but either way

Quoted 11 lines

ah, I didn't know that. My first intention was to use "void *", but then I searched over QEMU codebase
and found a few gpointer casts in exact use-case (with g_ptr_array_add).

I can correct this and resend if you want ;)

Quoted 4 lines

-- PMM

Thanks,

Added to nvme.next with the gpointer cast changed to void *.

#6 lore raw

Am Di., 28. Juli 2026 um 09:43 Uhr schrieb Klaus Jensen <[email protected]>:

Quoted 63 lines · Alexander Mikhalitsyn
Quoted 61 lines

Am Do., 9. Juli 2026 um 10:58 Uhr schrieb Peter Maydell <[email protected]>:

Quoted 44 lines
Quoted 41 lines · Alexander Mikhalitsyn

Am Do., 9. Juli 2026 um 10:51 Uhr schrieb Peter Maydell <[email protected]>:

Quoted 23 lines
Quoted 19 lines · Alexander Mikhalitsyn

From: Alexander Mikhalitsyn <[email protected]>

Let's use GPtrArray to build a list of blocker features and then g_strjoinv() to build a final comma-delimited string.

While previous approach was technically correct, it is fragile (because we need to take care of static buffer size choice) and Coverity dislikes it too.

Note, that we use g_ptr_array_new() to allocate array which means that GDestroyNotify callback is not set, so we can pass pointers to a static memory like g_ptr_array_add(..., (gpointer) "SR-IOV") without any problems as there won't be any attempt to free that memory.

Resolves: Coverity CID 1663673

---

Quoted 9 lines

hw/nvme/ctrl.c static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev, · line 9409

Quoted 8 lines
hw/nvme/ctrl.c +2 -2
@@ -9416,15 +9405,15 @@ static bool nvme_set_migration_blockers(NvmeCtrl *n, PCIDevice *pci_dev,
             }
 
             if (namespaces_num > 1) {
-                nvme_add_blocker_feature(blocker_features,
-                                         "Namespace Attachment");
+                g_ptr_array_add(blocker_features,
+                                         (gpointer) "Namespace Attachment");

Hi Peter,

Quoted 2 lines

Is the cast here because we're dropping the const property of the literal string?

Quoted 7 lines
Quoted 1 line

yep.

Mmm. Unfortunate but unavoidable I guess.

The glib docs recommend using "void *" instead of "gpointer" in new code: https://docs.gtk.org/glib/types.html#gpointer
but either way

ah, I didn't know that. My first intention was to use "void *", but then I searched over QEMU codebase
and found a few gpointer casts in exact use-case (with g_ptr_array_add).

I can correct this and resend if you want ;)

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

-- PMM

Thanks,

Added to nvme.next with the gpointer cast changed to void *.

Thank you, Klaus ;-)

Quoted 2 lines