[PATCH] newlib: testsuite: Fix source verification in memcpy-1

xuhang12120@gmail.com xuhang12120@gmail.com
Sat Dec 5 03:19:17 GMT 2026


From: xuhang <xuhang12120@gmail.com>

The source verification loop indexes both buffers with i instead of
its loop variable j. As a result, it checks only the last offset used
by the destination check and can miss changes elsewhere in the source.

Check every source byte against its saved value. Also use the saved
source to validate the destination, so changing both buffers in the
same way cannot mask a bad copy.

Tested on riscv32-unknown-elf under QEMU. The test passes with the
normal implementation. When a test double corrupts byte zero in both
buffers after a two-byte copy, the old test passes and the fixed test
reports both errors.
---
 newlib/testsuite/newlib.string/memcpy-1.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/newlib/testsuite/newlib.string/memcpy-1.c b/newlib/testsuite/newlib.string/memcpy-1.c
index 145db1a..f2ce980 100644
--- a/newlib/testsuite/newlib.string/memcpy-1.c
+++ b/newlib/testsuite/newlib.string/memcpy-1.c
@@ -114,8 +114,7 @@ main (void)
                          "(ret is %p, dest is %p)\n",
                          n, sa, da, ret, dest + START_COPY + da);
           
-          /* Check that content of the destination buffer
-             is the same as the source buffer, and
+          /* Check that the destination matches the original source, and
              memory outside destination buffer is not modified.  */
           for (j = 0; j < BUFF_SIZE; j++)
             if (j < START_COPY + da)
@@ -128,12 +127,12 @@ main (void)
               }
             else if (j < START_COPY + da + n)
               {
-                i = j - START_COPY - da;
-                if (dest[j] != (src + sa)[i])
+                unsigned offset = j - START_COPY - da;
+                if (dest[j] != backup_src[sa + offset])
                   print_error ("\nFailed: after memcpy of %u bytes "
                                "with src_align %u and dst_align %u, "
                                "byte %u in dest and src are not the same.\n",
-                               n, sa, da, i);
+                               n, sa, da, offset);
               }
             else if (dest[j] != 0)
               {
@@ -145,7 +144,7 @@ main (void)
 
           /* Check src is not modified.  */
           for (j = 0; j < BUFF_SIZE; j++)
-            if (src[i] != backup_src[i])
+            if (src[j] != backup_src[j])
               print_error ("\nFailed: after memcpy of %u bytes "
                            "with src_align %u and dst_align %u, "
                            "byte %u of src is modified.\n",

base-commit: 198e539ff07a465ba46c2802efdcf7372b8ccfc3


More information about the Newlib mailing list