Skip to contentSkip to conversation

tests/qtest/libqtest: Use GLib functions for proper const correctness

PATCH In review Amit Machhiwal 2 participants 4 messages May 22 – Aug 13

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tests/qtest/libqtest: Use GLib functions for proper const correctness

While commit e68da5b7a2cd ("tests/qtest: fix discarded const qualifier warning") addressed the immediate strstr() warning by making 'found' const, there's still a room for improvement: getenv() returns char *, but environment strings are semantically read-only and should be treated as const throughout their lifetime.

Replace getenv() with g_getenv() and strstr() with g_strstr_len() to maintain const correctness from source to use. This approach:

- Uses g_getenv() which returns const gchar *, matching the read-only semantics of environment variables
- Employs g_strstr_len() for consistent use of GLib string functions, aligning with QEMU conventions
- Eliminates all const-correctness warnings with strict compilers

---
v2:
- Rebased on top of e68da5b7a2cd ("tests/qtest: fix discarded const qualifier warning")
- Split from previous patch series and sent as standalone patch
- Updated commit message to clarify relationship with prior fix
- Retaining tags from previous version as no major changes

 tests/qtest/libqtest.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
tests/qtest/libqtest.c +4 -5
index b1e06ea364ec..cb80ab23182a 100644
@@ -2145,8 +2145,7 @@ bool mkimg(const char *file, const char *fmt, unsigned size_mb)
 
 bool qtest_verbose(const char *domain)
 {
-    const char *log = getenv("QTEST_LOG");
-    const char *found;
+    const gchar *found, *log = g_getenv("QTEST_LOG");
 
     assert(domain);
 
@@ -2172,11 +2171,11 @@ bool qtest_verbose(const char *domain)
          *  QTEST_LOG=<domain1>,-<domain2> (only false for domain2)
          *  allows other separators, except - and +
          */
-        found = strstr(log, domain);
+        found = g_strstr_len(log, -1, domain);
 
         if (found) {
             /* reject options given twice */
-            assert(!strstr(found + strlen(domain), domain));
+            assert(!g_strstr_len(found + strlen(domain), -1, domain));
 
             if (found > log) {
                 ptrdiff_t i = found - log - 1;
@@ -2190,7 +2189,7 @@ bool qtest_verbose(const char *domain)
              * If filtering out a specific domain, all others are
              * enabled.
              */
-            return !!strstr(log, "-");
+            return !!g_strstr_len(log, -1, "-");
         }
     }
 

base-commit: f5a2438405d4ae8b62de7c9b39fac0b2155ee544

Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

Hi Fabiano, Laurent,

Did you have any review comments on this patch?

Shall this be picked via your tree?

Thanks,
Amit

Quoted 71 lines · Amit Machhiwal

While commit e68da5b7a2cd ("tests/qtest: fix discarded const qualifier warning") addressed the immediate strstr() warning by making 'found' const, there's still a room for improvement: getenv() returns char *, but environment strings are semantically read-only and should be treated as const throughout their lifetime.

Replace getenv() with g_getenv() and strstr() with g_strstr_len() to maintain const correctness from source to use. This approach:

- Uses g_getenv() which returns const gchar *, matching the read-only semantics of environment variables
- Employs g_strstr_len() for consistent use of GLib string functions, aligning with QEMU conventions
- Eliminates all const-correctness warnings with strict compilers

---
v2:
- Rebased on top of e68da5b7a2cd ("tests/qtest: fix discarded const qualifier warning")
- Split from previous patch series and sent as standalone patch
- Updated commit message to clarify relationship with prior fix
- Retaining tags from previous version as no major changes

 tests/qtest/libqtest.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
tests/qtest/libqtest.c +4 -5
index b1e06ea364ec..cb80ab23182a 100644
@@ -2145,8 +2145,7 @@ bool mkimg(const char *file, const char *fmt, unsigned size_mb)
 
 bool qtest_verbose(const char *domain)
 {
-    const char *log = getenv("QTEST_LOG");
-    const char *found;
+    const gchar *found, *log = g_getenv("QTEST_LOG");
 
     assert(domain);
 
@@ -2172,11 +2171,11 @@ bool qtest_verbose(const char *domain)
          *  QTEST_LOG=<domain1>,-<domain2> (only false for domain2)
          *  allows other separators, except - and +
          */
-        found = strstr(log, domain);
+        found = g_strstr_len(log, -1, domain);
 
         if (found) {
             /* reject options given twice */
-            assert(!strstr(found + strlen(domain), domain));
+            assert(!g_strstr_len(found + strlen(domain), -1, domain));
 
             if (found > log) {
                 ptrdiff_t i = found - log - 1;
@@ -2190,7 +2189,7 @@ bool qtest_verbose(const char *domain)
              * If filtering out a specific domain, all others are
              * enabled.
              */
-            return !!strstr(log, "-");
+            return !!g_strstr_len(log, -1, "-");
         }
     }
 

base-commit: f5a2438405d4ae8b62de7c9b39fac0b2155ee544

Signature · 1 line
2.50.1 (Apple Git-155)
#2 lore raw
Quoted 6 lines · Amit Machhiwal

Hi Fabiano, Laurent,

Did you have any review comments on this patch?

Shall this be picked via your tree?

I'll take it, thanks.

#3 lore raw

Hi Fabiano,

Quoted 10 lines · Fabiano Rosas
Quoted 6 lines · Amit Machhiwal

Hi Fabiano, Laurent,

Did you have any review comments on this patch?

Shall this be picked via your tree?

I'll take it, thanks.

I do not see it merged yet. Has it been queued yet?

Thanks,
Amit