@samitouri / QOSamiQemu / commits / c54c041ae0

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 Tested-by: Anushree Mathur <anushree.mathur@linux.ibm.com> Reviewed-by: Aditya Gupta <adityag@linux.ibm.com> Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com> Signed-off-by: Fabiano Rosas <farosas@suse.de>

Amit Machhiwal committed May 22, 2026 at 13:55 UTC c54c041ae09004866ae049594ba9fb3a3c5ac271
1 file changed +4 -5
tests/qtest/libqtest.c
+4 -5
@@ -2151,8 +2151,7 @@ bool mkimg(const char *file, const char *fmt, unsigned size_mb)
2151
2152 bool qtest_verbose(const char *domain)
2153 {
2154 - const char *log = getenv("QTEST_LOG");
2155 - const char *found;
2154 + const gchar *found, *log = g_getenv("QTEST_LOG");
2155
2156 assert(domain);
2157
@@ -2178,11 +2177,11 @@ bool qtest_verbose(const char *domain)
2177 * QTEST_LOG=<domain1>,-<domain2> (only false for domain2)
2178 * allows other separators, except - and +
2179 */
2181 - found = strstr(log, domain);
2180 + found = g_strstr_len(log, -1, domain);
2181
2182 if (found) {
2183 /* reject options given twice */
2185 - assert(!strstr(found + strlen(domain), domain));
2184 + assert(!g_strstr_len(found + strlen(domain), -1, domain));
2185
2186 if (found > log) {
2187 ptrdiff_t i = found - log - 1;
@@ -2196,7 +2195,7 @@ bool qtest_verbose(const char *domain)
2195 * If filtering out a specific domain, all others are
2196 * enabled.
2197 */
2199 - return !!strstr(log, "-");
2198 + return !!g_strstr_len(log, -1, "-");
2199 }
2200 }
2201