Skip to contentSkip to conversation

target/riscv/riscv-qmp-cmds.c: coverity-related fixes

PATCH Applied Daniel Henrique Barboza 2 participants 3 messages Oct 22, 2025 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 target/riscv/riscv-qmp-cmds.c: coverity-related fixes

Coverity CID 1641401 reports that, in reg_is_ulong_integer(), we're dereferencing a NULL pointer in "reg1" when using it in strcasecmp() call. A similar case is reported with CID 1641393.

In theory that will never happen - it's guaranteed that both "reg1" and "reg2" is non-NULL because we're retrieving them in compile-time from static arrays. Coverity doesn't know that though.

To make Coverity happier and add a bit more clarity in the code, g_assert() each token to make it clear that those 2 values aren't supposed to be NULL ever. Do that in both reg_is_ulong_integer() and reg_is_u64_fpu().

We're also taking the opportunity to implement suggestions made by Peter in [1] in both functions:

- use g_strsplit() instead of strtok();
- use g_ascii_strcasecmp() instead of strcasecmp().

[1] https://lore.kernel.org/qemu-devel/CAFEAcA_y4bwd9GANbXnpTy2mv80Vg_jp+A-VkQS5V6f0+BFRAA@mail.gmail.com/

Coverity: CID 1641393, 1641401

Fixes e06d209aa6 ("target/riscv: implement MonitorDef HMP API")

---

Changes from v1:
- use g_auto(GStrv) instead of g_autofree
- v1 link: https://lore.kernel.org/qemu-riscv/[email protected]/

 target/riscv/riscv-qmp-cmds.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
target/riscv/riscv-qmp-cmds.c +12 -10
index c499f9b9a7..d5e9bec0f8 100644
@@ -273,12 +273,13 @@ static bool reg_is_ulong_integer(CPURISCVState *env, const char *name,
     }
 
     for (int i = 0; i < 32; i++) {
-        g_autofree char *reg_name = g_strdup(reg_names[i]);
-        char *reg1 = strtok(reg_name, "/");
-        char *reg2 = strtok(NULL, "/");
+        g_auto(GStrv) reg_name = g_strsplit(reg_names[i], "/", 2);
 
-        if (strcasecmp(reg1, name) == 0 ||
-            (reg2 && strcasecmp(reg2, name) == 0)) {
+        g_assert(reg_name[0]);
+        g_assert(reg_name[1]);
+
+        if (g_ascii_strcasecmp(reg_name[0], name) == 0 ||
+            g_ascii_strcasecmp(reg_name[1], name) == 0) {
             *val = vals[i];
             return true;
         }
@@ -294,12 +295,13 @@ static bool reg_is_u64_fpu(CPURISCVState *env, const char *name, uint64_t *val)
     }
 
     for (int i = 0; i < 32; i++) {
-        g_autofree char *reg_name = g_strdup(riscv_fpr_regnames[i]);
-        char *reg1 = strtok(reg_name, "/");
-        char *reg2 = strtok(NULL, "/");
+        g_auto(GStrv) reg_name = g_strsplit(riscv_fpr_regnames[i], "/", 2);
+
+        g_assert(reg_name[0]);
+        g_assert(reg_name[1]);
 
-        if (strcasecmp(reg1, name) == 0 ||
-            (reg2 && strcasecmp(reg2, name) == 0)) {
+        if (g_ascii_strcasecmp(reg_name[0], name) == 0 ||
+            g_ascii_strcasecmp(reg_name[1], name) == 0) {
             *val = env->fpr[i];
             return true;
         }
Signature · 1 line
2.51.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 20 lines · Daniel Henrique Barboza

Coverity CID 1641401 reports that, in reg_is_ulong_integer(), we're dereferencing a NULL pointer in "reg1" when using it in strcasecmp() call. A similar case is reported with CID 1641393.

In theory that will never happen - it's guaranteed that both "reg1" and "reg2" is non-NULL because we're retrieving them in compile-time from static arrays. Coverity doesn't know that though.

To make Coverity happier and add a bit more clarity in the code, g_assert() each token to make it clear that those 2 values aren't supposed to be NULL ever. Do that in both reg_is_ulong_integer() and reg_is_u64_fpu().

We're also taking the opportunity to implement suggestions made by Peter in [1] in both functions:

- use g_strsplit() instead of strtok();
- use g_ascii_strcasecmp() instead of strcasecmp().

Quoted 6 lines

[1] https://lore.kernel.org/qemu-devel/CAFEAcA_y4bwd9GANbXnpTy2mv80Vg_jp+A-VkQS5V6f0+BFRAA@mail.gmail.com/

Coverity: CID 1641393, 1641401

Fixes e06d209aa6 ("target/riscv: implement MonitorDef HMP API")

Alistair

Quoted 56 lines

---

Changes from v1:
- use g_auto(GStrv) instead of g_autofree
- v1 link: https://lore.kernel.org/qemu-riscv/[email protected]/

 target/riscv/riscv-qmp-cmds.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
target/riscv/riscv-qmp-cmds.c +12 -11
index c499f9b9a7..d5e9bec0f8 100644
@@ -273,12 +273,13 @@ static bool reg_is_ulong_integer(CPURISCVState *env, const char *name,
     }
 
     for (int i = 0; i < 32; i++) {
-        g_autofree char *reg_name = g_strdup(reg_names[i]);
-        char *reg1 = strtok(reg_name, "/");
-        char *reg2 = strtok(NULL, "/");
+        g_auto(GStrv) reg_name = g_strsplit(reg_names[i], "/", 2);
 
-        if (strcasecmp(reg1, name) == 0 ||
-            (reg2 && strcasecmp(reg2, name) == 0)) {
+        g_assert(reg_name[0]);
+        g_assert(reg_name[1]);
+
+        if (g_ascii_strcasecmp(reg_name[0], name) == 0 ||
+            g_ascii_strcasecmp(reg_name[1], name) == 0) {
             *val = vals[i];
             return true;
         }
@@ -294,12 +295,13 @@ static bool reg_is_u64_fpu(CPURISCVState *env, const char *name, uint64_t *val)
     }
 
     for (int i = 0; i < 32; i++) {
-        g_autofree char *reg_name = g_strdup(riscv_fpr_regnames[i]);
-        char *reg1 = strtok(reg_name, "/");
-        char *reg2 = strtok(NULL, "/");
+        g_auto(GStrv) reg_name = g_strsplit(riscv_fpr_regnames[i], "/", 2);
+
+        g_assert(reg_name[0]);
+        g_assert(reg_name[1]);
 
-        if (strcasecmp(reg1, name) == 0 ||
-            (reg2 && strcasecmp(reg2, name) == 0)) {
+        if (g_ascii_strcasecmp(reg_name[0], name) == 0 ||
+            g_ascii_strcasecmp(reg_name[1], name) == 0) {
             *val = env->fpr[i];
             return true;
         }
--

2.51.0

#2 lore raw
In reply to Daniel Henrique Barboza
Quoted 20 lines · Daniel Henrique Barboza

Coverity CID 1641401 reports that, in reg_is_ulong_integer(), we're dereferencing a NULL pointer in "reg1" when using it in strcasecmp() call. A similar case is reported with CID 1641393.

In theory that will never happen - it's guaranteed that both "reg1" and "reg2" is non-NULL because we're retrieving them in compile-time from static arrays. Coverity doesn't know that though.

To make Coverity happier and add a bit more clarity in the code, g_assert() each token to make it clear that those 2 values aren't supposed to be NULL ever. Do that in both reg_is_ulong_integer() and reg_is_u64_fpu().

We're also taking the opportunity to implement suggestions made by Peter in [1] in both functions:

- use g_strsplit() instead of strtok();
- use g_ascii_strcasecmp() instead of strcasecmp().

Quoted 6 lines

[1] https://lore.kernel.org/qemu-devel/CAFEAcA_y4bwd9GANbXnpTy2mv80Vg_jp+A-VkQS5V6f0+BFRAA@mail.gmail.com/

Coverity: CID 1641393, 1641401

Fixes e06d209aa6 ("target/riscv: implement MonitorDef HMP API")

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 56 lines

---

Changes from v1:
- use g_auto(GStrv) instead of g_autofree
- v1 link: https://lore.kernel.org/qemu-riscv/[email protected]/

 target/riscv/riscv-qmp-cmds.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
target/riscv/riscv-qmp-cmds.c +12 -11
index c499f9b9a7..d5e9bec0f8 100644
@@ -273,12 +273,13 @@ static bool reg_is_ulong_integer(CPURISCVState *env, const char *name,
     }
 
     for (int i = 0; i < 32; i++) {
-        g_autofree char *reg_name = g_strdup(reg_names[i]);
-        char *reg1 = strtok(reg_name, "/");
-        char *reg2 = strtok(NULL, "/");
+        g_auto(GStrv) reg_name = g_strsplit(reg_names[i], "/", 2);
 
-        if (strcasecmp(reg1, name) == 0 ||
-            (reg2 && strcasecmp(reg2, name) == 0)) {
+        g_assert(reg_name[0]);
+        g_assert(reg_name[1]);
+
+        if (g_ascii_strcasecmp(reg_name[0], name) == 0 ||
+            g_ascii_strcasecmp(reg_name[1], name) == 0) {
             *val = vals[i];
             return true;
         }
@@ -294,12 +295,13 @@ static bool reg_is_u64_fpu(CPURISCVState *env, const char *name, uint64_t *val)
     }
 
     for (int i = 0; i < 32; i++) {
-        g_autofree char *reg_name = g_strdup(riscv_fpr_regnames[i]);
-        char *reg1 = strtok(reg_name, "/");
-        char *reg2 = strtok(NULL, "/");
+        g_auto(GStrv) reg_name = g_strsplit(riscv_fpr_regnames[i], "/", 2);
+
+        g_assert(reg_name[0]);
+        g_assert(reg_name[1]);
 
-        if (strcasecmp(reg1, name) == 0 ||
-            (reg2 && strcasecmp(reg2, name) == 0)) {
+        if (g_ascii_strcasecmp(reg_name[0], name) == 0 ||
+            g_ascii_strcasecmp(reg_name[1], name) == 0) {
             *val = env->fpr[i];
             return true;
         }
--

2.51.0