[PATCH 2/2] string: Test strrchr character argument conversion

Chengrong Dai daichengrong@iscas.ac.cn
Mon Nov 23 08:11:38 GMT 2026


From: daichengrong <daichengrong@iscas.ac.cn>

Add coverage for strrchr character argument conversion, including
nonzero integers that convert to NUL, regular characters and high-byte
values.  Include the terminating NUL when computing the expected result.

Co-authored-by: Yuansheng <yuansheng@isrc.iscas.ac.cn>
Signed-off-by: daichengrong <daichengrong@iscas.ac.cn>
---
 string/test-strrchr.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/string/test-strrchr.c b/string/test-strrchr.c
index 5fad54ae..a6139b59 100644
--- a/string/test-strrchr.c
+++ b/string/test-strrchr.c
@@ -198,6 +198,41 @@ do_random_tests (void)
     }
 }
 
+#ifndef WIDE
+/* The character argument is converted to char before searching, including
+   when a nonzero int converts to the terminating NUL.  */
+static void
+do_char_conversion_tests (void)
+{
+  const int chars[] = { 0, 256, -256, 512, -512, 'b',
+                       'b' + 256, 'b' - 256, 255, -1, 511, -257 };
+
+  for (size_t len = 0; len <= 256; ++len)
+    for (size_t align = 0; align < 32; ++align)
+      {
+        char *s = (char *) buf1 + align;
+        memset (s, 'a', len);
+        if (len > 1)
+          s[1] = 'b';
+        if (len > 3)
+          s[len - 2] = 'b';
+        if (len > 2)
+          s[len - 1] = (char) 255;
+        s[len] = '\0';
+
+        for (size_t i = 0; i < sizeof chars / sizeof chars[0]; ++i)
+          {
+            char *expected = NULL;
+            for (size_t j = 0; j <= len; ++j)
+              if (s[j] == (char) chars[i])
+                expected = s + j;
+            FOR_EACH_IMPL (impl, 0)
+              do_one_test (impl, s, chars[i], expected);
+          }
+      }
+}
+#endif
+
 int
 test_main (void)
 {
@@ -246,6 +281,9 @@ test_main (void)
       do_test (0, i, i + 1, 0, BIG_CHAR);
     }
 
+#ifndef WIDE
+  do_char_conversion_tests ();
+#endif
   do_random_tests ();
   return ret;
 }



More information about the Libc-alpha mailing list