[PATCH] Avoid add an integer to a string

H.J. Lu hjl.tools@gmail.com
Mon Dec 16 21:50:23 GMT 2024


Clang 19 issues an error:

tst-iconv-sticky-input-error.c:125:42: error: adding 'int' to a string does not append to the string [-Werror,-Wstring-plus-int]
  125 |                 expected_output = "ABXY" + skip;
      |                                   ~~~~~~~^~~~~~
tst-iconv-sticky-input-error.c:125:42: note: use array indexing to silence this warning
  125 |                 expected_output = "ABXY" + skip;
      |                                          ^
      |                                   &      [     ]

Use array indexing instead.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 dirent/tst-readdir-long.c            | 2 +-
 dirent/tst-rewinddir.c               | 2 +-
 iconv/tst-iconv-sticky-input-error.c | 6 +++---
 posix/bug-regex24.c                  | 4 ++--
 4 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/dirent/tst-readdir-long.c b/dirent/tst-readdir-long.c
index 409318fa52..18c8521d28 100644
--- a/dirent/tst-readdir-long.c
+++ b/dirent/tst-readdir-long.c
@@ -59,7 +59,7 @@ add_directory_entry (struct support_fuse_dirstream *d, uint64_t offset)
   if (offset <= 1)
     {
       type = DT_DIR;
-      name = ".." + !offset;    /* "." or "..".  */
+      name = &".."[!offset];    /* "." or "..".  */
       ino = 1;
     }
   else if (length == 1000)
diff --git a/dirent/tst-rewinddir.c b/dirent/tst-rewinddir.c
index 1479766ebe..8e24d2737a 100644
--- a/dirent/tst-rewinddir.c
+++ b/dirent/tst-rewinddir.c
@@ -30,7 +30,7 @@ static char *
 name_at_offset (unsigned int offset)
 {
   if (offset <= 1)
-    return xstrdup (".." + !offset); /* "." or "..".  */
+    return xstrdup (&".."[!offset]); /* "." or "..".  */
   else
     /* Pad the name with a lot of zeros, so that the dirent buffer gets
        filled more quickly.  */
diff --git a/iconv/tst-iconv-sticky-input-error.c b/iconv/tst-iconv-sticky-input-error.c
index 34a245f185..d82d885023 100644
--- a/iconv/tst-iconv-sticky-input-error.c
+++ b/iconv/tst-iconv-sticky-input-error.c
@@ -122,11 +122,11 @@ do_test (void)
             {
               const char *expected_output;
               if (do_ignore || strstr (charsets[to_idx], "//IGNORE") != NULL)
-                expected_output = "ABXY" + skip;
+                expected_output = &"ABXY"[skip];
               else
-                expected_output = "AB" + skip;
+                expected_output = &"AB"[skip];
               one_direction (charsets[from_idx], charsets[to_idx], do_ignore,
-                             "AB\xffXY" + skip, expected_output, limit);
+                             &"AB\xffXY"[skip], expected_output, limit);
             }
 
   return 0;
diff --git a/posix/bug-regex24.c b/posix/bug-regex24.c
index 97c5c3508a..f5897746e9 100644
--- a/posix/bug-regex24.c
+++ b/posix/bug-regex24.c
@@ -45,10 +45,10 @@ do_test (void)
       {
 	int len = m[i].rm_eo - m[i].rm_so;
 
-	printf ("m[%d] = \"%.*s\"\n", i, len, str + m[i].rm_so);
+	printf ("m[%d] = \"%.*s\"\n", i, len, &str[m[i].rm_so]);
 
 	if (strlen (expected[i]) != len
-	    || memcmp (expected[i], str + m[i].rm_so, len) != 0)
+	    || memcmp (expected[i], &str[m[i].rm_so], len) != 0)
 	  result = 1;
       }
 
-- 
2.47.1



More information about the Libc-alpha mailing list