Hi Aaron,

On 30/08/17 15:37, Aaron Sawdey wrote:
On Wed, 2017-08-30 at 10:16 +0100, Renlin Li wrote:
Hi,


Hi,
   Renlin you are correct that it shouldn't be using strcpy because the
string may not be null terminated. However I would suggest we use
memcpy instead of strncpy. The reason is that cases where there is a
null char in the middle of the string test whether the strncmp is
properly ignoring what comes after. So how about using this:

          memcpy(a,str1,SZ);                                            \
          memcpy(b,str2,SZ);                                            \

as in the test_memcmp_ part of the macro?

You are right.

For strncpy, if there is a null-character before size, the destination will be padded with zeros.

memcpy is better than strncpy in this case.
Here is the updated patch.

Regards,
Renlin
diff --git a/gcc/testsuite/gcc.dg/memcmp-1.c b/gcc/testsuite/gcc.dg/memcmp-1.c
index 828a0ca..d258354 100644
--- a/gcc/testsuite/gcc.dg/memcmp-1.c
+++ b/gcc/testsuite/gcc.dg/memcmp-1.c
@@ -110,8 +110,8 @@ static void test_strncmp_ ## SZ ## _ ## ALIGN (const char *str1, const char *str
 	{								\
 	  a = three+i*ALIGN+j*(4096-2*i*ALIGN);				\
 	  b = four+i*ALIGN+j*(4096-2*i*ALIGN);				\
-	  strcpy(a,str1);						\
-	  strcpy(b,str2);						\
+	  memcpy(a,str1,SZ);						\
+	  memcpy(b,str2,SZ);						\
 	  r = strncmp(a,b,SZ);						\
 	  if ( r < 0 && !(expect < 0) ) abort();			\
 	  if ( r > 0 && !(expect > 0) )	abort();			\

Reply via email to