]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
Avoid string literal type confusion
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 8 May 2026 22:35:18 +0000 (15:35 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 9 May 2026 03:25:26 +0000 (20:25 -0700)
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.

ChangeLog
lib/boot-time.c
lib/time_rz.c
tests/test-posix_spawn-chdir.c
tests/test-posix_spawn-fchdir.c
tests/test-unsetenv.c

index 518d0bfe17d0b3c7ea12a8c4fc3aaa7bcb1d2b1f..b16c21f04e9e023f686e735e899a875964d51e50 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2026-05-08  Paul Eggert  <eggert@cs.ucla.edu>
 
+       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.
index dee975fa4e207b3b1c5a77e5b89032a149e659e6..ae305a18067a93a56ead6b08b1f672fcef7cc334 100644 (file)
@@ -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 ();
 
index 0e27b2d2716ac449c3c0dfeb0c906d1d3e7f5924..03aa3e6700ece00c5ae811145ddde475877d1a0c 100644 (file)
@@ -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)))
index e169949b951af2b98abddc0f938d59f860af15b5..4df9259cbe41c39a6cde6c7b044593b39e9d7bf9 100644 (file)
@@ -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;
index 9ae271f8c9f63a5667ff27777f3d62d7a844a8b6..f956aa9ca6881d943a9760ef10c25e051189f5b3 100644 (file)
@@ -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"
index 242d17e7e1f813ad502c5a350a8cabe10a72e31e..aed2eea3a8d5fd0316009d087e4e12e44b83c65d 100644 (file)
@@ -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.  */