From: Paul Eggert Date: Fri, 8 May 2026 22:35:18 +0000 (-0700) Subject: Avoid string literal type confusion X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=40edc7743be0222ae4042008457c1bdff207f598;p=thirdparty%2Fgnulib.git Avoid string literal type confusion Instead of ‘... (char *) "abc" ...’, which is a bit confusing as "abc" is already of type char *, use ‘static char const s[] = "abc"; ... (char *) s .... This pacifices -Wuseless-cast. * lib/boot-time.c (get_boot_time_uncached): * lib/time_rz.c (save_abbr): * tests/test-posix_spawn-chdir.c (test): * tests/test-posix_spawn-fchdir.c (test): * tests/test-unsetenv.c (main): Redo with named string as described above. --- diff --git a/ChangeLog b/ChangeLog index 518d0bfe17..b16c21f04e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2026-05-08 Paul Eggert + Avoid string literal type confusion + Instead of ‘... (char *) "abc" ...’, which is a bit confusing as + "abc" is already of type char *, use ‘static char const s[] = + "abc"; ... (char *) s .... This pacifices -Wuseless-cast. + * lib/boot-time.c (get_boot_time_uncached): + * lib/time_rz.c (save_abbr): + * tests/test-posix_spawn-chdir.c (test): + * tests/test-posix_spawn-fchdir.c (test): + * tests/test-unsetenv.c (main): + Redo with named string as described above. + times-tests: pacify -Wuseless-cast * tests/test-times.c (tms2ms): New function. (main): Use it to simplify printing and avoid need for casts. diff --git a/lib/boot-time.c b/lib/boot-time.c index dee975fa4e..ae305a1806 100644 --- a/lib/boot-time.c +++ b/lib/boot-time.c @@ -95,7 +95,8 @@ get_boot_time_uncached (struct timespec *p_boot_time) Solaris' utmpname returns 1 upon success -- which is contrary to what the GNU libc version does. In addition, older GNU libc versions are actually void. */ - UTMP_NAME_FUNCTION ((char *) UTMP_FILE); + static char const utmp_file[] = UTMP_FILE; + UTMP_NAME_FUNCTION ((char *) utmp_file); SET_UTMP_ENT (); diff --git a/lib/time_rz.c b/lib/time_rz.c index 0e27b2d271..03aa3e6700 100644 --- a/lib/time_rz.c +++ b/lib/time_rz.c @@ -118,7 +118,8 @@ save_abbr (timezone_t tz, struct tm *tm) { # if HAVE_STRUCT_TM_TM_ZONE char const *zone = tm->tm_zone; - char *zone_copy = (char *) ""; + static char const mt[] = ""; + char *zone_copy = (char *) mt; /* No need to replace null zones, or zones within the struct tm. */ if (!zone || ((char *) tm <= zone && zone < (char *) (tm + 1))) diff --git a/tests/test-posix_spawn-chdir.c b/tests/test-posix_spawn-chdir.c index e169949b95..4df9259cbe 100644 --- a/tests/test-posix_spawn-chdir.c +++ b/tests/test-posix_spawn-chdir.c @@ -50,7 +50,8 @@ fd_safer (int fd) static void test (const char *pwd_prog) { - char *argv[2] = { (char *) "pwd", NULL }; + static char const pwd[] = "pwd"; + char *argv[2] = { (char *) pwd, NULL }; int ifd[2]; sigset_t blocked_signals; sigset_t fatal_signal_set; diff --git a/tests/test-posix_spawn-fchdir.c b/tests/test-posix_spawn-fchdir.c index 9ae271f8c9..f956aa9ca6 100644 --- a/tests/test-posix_spawn-fchdir.c +++ b/tests/test-posix_spawn-fchdir.c @@ -54,7 +54,8 @@ fd_safer (int fd) static void test (const char *pwd_prog) { - char *argv[2] = { (char *) "pwd", NULL }; + static char const pwd[] = "pwd"; + char *argv[2] = { (char *) pwd, NULL }; /* The name of a directory that most likely is accessible. */ #if defined __ANDROID__ #define KNOWNDIR "/proc" diff --git a/tests/test-unsetenv.c b/tests/test-unsetenv.c index 242d17e7e1..aed2eea3a8 100644 --- a/tests/test-unsetenv.c +++ b/tests/test-unsetenv.c @@ -36,7 +36,8 @@ main (void) static char entry[] = "b=2"; /* Test removal when multiple entries present. */ - ASSERT (putenv ((char *) "a=1") == 0); + static char const a_equals_1[] = "a=1"; + ASSERT (putenv ((char *) a_equals_1) == 0); ASSERT (putenv (entry) == 0); entry[0] = 'a'; /* Unspecified what getenv("a") would be at this point. */ ASSERT (unsetenv ("a") == 0); /* Both entries will be removed. */