Using strlcpy if target OS allows it

Magnus Fromreide magfr@lysator.liu.se
Fri May 6 14:33:00 GMT 2005


On Fri, May 06, 2005 at 10:34:12AM +0200, Paolo Carlini wrote:
> Magnus Fromreide wrote:
> 
> >>Some comments:
> >>1- Let's start with mainline and 4_0-branch, that have a few more
> >>testcases that should be also patched. Can you prepare a second patch
> >>targeted at mainline/4_0-branch?
> >>    
> >>
> >Eh, it is targeted towards mainline already.
> >The reson I choose not to change 21_strings/c_strings/char/1.cc and 2.cc is
> >that I think those tests are intended to test str* functions and then it
> >would be a bad idea to replace them.
> >  
> >
> Tests are missing, those in wchar_t directories in mainline/4_0, for
> example,
> 27_io/basic_streambuf/sputn/wchar_t/1.cc, and so on. Please complete the
> work.

Is this what you meant?

The hard part was in figuring out that you were complaining of the calls
to wcscpy, I thought I had already handled all strcpy's.

> >>3- You should regtest the changes and provide a ChangeLog. If you can't
> >>do that additional (boring ;) work, I will, but only in a few days.
> >>
> >Oh dear.
> >I do regtesting by building the c,c++ and java languages with and without my
> >patch and then compare the output. Is there a better way, and how?
> >
> There isn't, but you should not do that in secret ;) OK.

Alas, I did it once more in secret. I doubt one more i686-linux tester would
matter much.

/MF
-------------- next part --------------
Index: libstdc++-v3/ChangeLog
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/ChangeLog,v
retrieving revision 1.2991
diff -u -r1.2991 ChangeLog
--- libstdc++-v3/ChangeLog	5 May 2005 16:10:01 -0000	1.2991
+++ libstdc++-v3/ChangeLog	6 May 2005 12:58:32 -0000
@@ -1,3 +1,24 @@
+2005-05-06  Magnus Fromreide  <magfr@lysator.liu.se>
+
+	* config/locale/generic/c_locale.h: Use memcpy instead of strcpy
+	as the length of the string is known.
+	* config/locale/generic/time_members.h: Likewise
+	* config/locale/gnu/c_locale.h: Likewise
+	* config/locale/gnu/messages_members.h: Likewise
+	* config/locale/gnu/time_members.h: Likewise
+	* testsuite/22_locale/codecvt/unshift/char/1.cc: Likewise
+	* testsuite/22_locale/codecvt/unshift/wchar_t/1.cc: Likewise
+
+	* testsuite/27_io/basic_streambuf/overflow/char/1.cc: Use
+	initialization instead of copying as the string is used only once.
+	* testsuite/27_io/basic_streambuf/overflow/wchar_t/1.cc: Likewise
+	* testsuite/27_io/basic_streambuf/sgetc/char/1.cc: Likewise
+	* testsuite/27_io/basic_streambuf/sgetc/wchar_t/1.cc: Likewise
+	* testsuite/27_io/basic_streambuf/sgetn/char/1.cc: Likewise
+	* testsuite/27_io/basic_streambuf/sgetn/wchar_t/1.cc: Likewise
+	* testsuite/27_io/basic_streambuf/sputn/char/1.cc: Likewise
+	* testsuite/27_io/basic_streambuf/sputn/wchar_t/1.cc: Likewise
+
 2005-05-04  Benjamin Kosnik  <bkoz@redhat.com>
 
 	* acinclude.m4: Remove testsuite_wchar_t and testsuite_thread.
Index: libstdc++-v3/config/locale/generic/c_locale.h
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/config/locale/generic/c_locale.h,v
retrieving revision 1.14
diff -u -r1.14 c_locale.h
--- libstdc++-v3/config/locale/generic/c_locale.h	30 Jan 2005 14:09:58 -0000	1.14
+++ libstdc++-v3/config/locale/generic/c_locale.h	6 May 2005 12:58:32 -0000
@@ -63,8 +63,9 @@
       char* __sav = NULL;
       if (std::strcmp(__old, "C"))
 	{
-	  __sav = new char[std::strlen(__old) + 1];
-	  std::strcpy(__sav, __old);
+	  std::size_t __strsize = std::strlen(__old) + 1;
+	  __sav = new char[__strsize];
+	  std::memcpy(__sav, __old, __strsize);
 	  std::setlocale(LC_NUMERIC, "C");
 	}
 
Index: libstdc++-v3/config/locale/generic/time_members.h
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/config/locale/generic/time_members.h,v
retrieving revision 1.4
diff -u -r1.4 time_members.h
--- libstdc++-v3/config/locale/generic/time_members.h	2 Oct 2003 23:06:12 -0000	1.4
+++ libstdc++-v3/config/locale/generic/time_members.h	6 May 2005 12:58:33 -0000
@@ -55,8 +55,9 @@
 				     size_t __refs) 
     : facet(__refs), _M_data(NULL)
     { 
-      char* __tmp = new char[std::strlen(__s) + 1];
-      std::strcpy(__tmp, __s);
+      std::size_t __strsize = std::strlen(__s) + 1;
+      char* __tmp = new char[__strsize];
+      std::memcpy(__tmp, __s, __strsize);
       _M_name_timepunct = __tmp;
       _M_initialize_timepunct(__cloc); 
     }
Index: libstdc++-v3/config/locale/gnu/c_locale.h
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/config/locale/gnu/c_locale.h,v
retrieving revision 1.13
diff -u -r1.13 c_locale.h
--- libstdc++-v3/config/locale/gnu/c_locale.h	24 Mar 2005 05:45:43 -0000	1.13
+++ libstdc++-v3/config/locale/gnu/c_locale.h	6 May 2005 12:58:33 -0000
@@ -77,8 +77,9 @@
 		     _Tv __v, const __c_locale&, int __prec)
     {
       char* __old = std::setlocale(LC_ALL, NULL);
-      char* __sav = new char[std::strlen(__old) + 1];
-      std::strcpy(__sav, __old);
+      std::size_t __strsize = std::strlen(__old) + 1;
+      char* __sav = new char[__strsize];
+      std::memcpy(__sav, __old, __strsize);
       std::setlocale(LC_ALL, "C");
 #endif
 
Index: libstdc++-v3/config/locale/gnu/messages_members.h
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/config/locale/gnu/messages_members.h,v
retrieving revision 1.13
diff -u -r1.13 messages_members.h
--- libstdc++-v3/config/locale/gnu/messages_members.h	22 May 2004 23:46:31 -0000	1.13
+++ libstdc++-v3/config/locale/gnu/messages_members.h	6 May 2005 12:58:33 -0000
@@ -46,8 +46,9 @@
      : facet(__refs), _M_c_locale_messages(_S_clone_c_locale(__cloc)),
      _M_name_messages(__s)
      {
-       char* __tmp = new char[std::strlen(__s) + 1];
-       std::strcpy(__tmp, __s);
+       std::size_t __strsize = std::strlen(__s) + 1;
+       char* __tmp = new char[__strsize];
+       std::memcpy(__tmp, __s, __strsize);
        _M_name_messages = __tmp;
      }
 
@@ -92,8 +93,9 @@
      { 
        if (this->_M_name_messages != locale::facet::_S_get_c_name())
 	 delete [] this->_M_name_messages;
-       char* __tmp = new char[std::strlen(__s) + 1];
-       std::strcpy(__tmp, __s);
+       std::size_t __strsize = std::strlen(__s) + 1;
+       char* __tmp = new char[__strsize];
+       std::memcpy(__tmp, __s, __strsize);
        this->_M_name_messages = __tmp;
 
        if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
Index: libstdc++-v3/config/locale/gnu/time_members.h
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/config/locale/gnu/time_members.h,v
retrieving revision 1.5
diff -u -r1.5 time_members.h
--- libstdc++-v3/config/locale/gnu/time_members.h	22 May 2004 23:46:31 -0000	1.5
+++ libstdc++-v3/config/locale/gnu/time_members.h	6 May 2005 12:58:34 -0000
@@ -52,8 +52,9 @@
     : facet(__refs), _M_data(NULL), _M_c_locale_timepunct(NULL), 
     _M_name_timepunct(__s)
     { 
-      char* __tmp = new char[std::strlen(__s) + 1];
-      std::strcpy(__tmp, __s);
+      std::size_t __strsize = std::strlen(__s) + 1;
+      char* __tmp = new char[__strsize];
+      std::memcpy(__tmp, __s, __strsize);
       _M_name_timepunct = __tmp;
       _M_initialize_timepunct(__cloc); 
     }
Index: libstdc++-v3/testsuite/22_locale/codecvt/unshift/char/1.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/testsuite/22_locale/codecvt/unshift/char/1.cc,v
retrieving revision 1.2
diff -u -r1.2 1.cc
--- libstdc++-v3/testsuite/22_locale/codecvt/unshift/char/1.cc	23 Sep 2003 20:02:16 -0000	1.2
+++ libstdc++-v3/testsuite/22_locale/codecvt/unshift/char/1.cc	6 May 2005 12:58:38 -0000
@@ -67,7 +67,7 @@
   VERIFY( to_next == c_arr );
 
   // unshift
-  strcpy(c_arr, c_lit);
+  memcpy(c_arr, c_lit, strlen(c_lit) + 1);
   result r3 = cvt->unshift(state, c_arr, c_arr + size, to_next);
   VERIFY( r3 == codecvt_base::noconv );
   VERIFY( !strcmp(c_arr, c_lit) ); 
Index: libstdc++-v3/testsuite/22_locale/codecvt/unshift/wchar_t/1.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/testsuite/22_locale/codecvt/unshift/wchar_t/1.cc,v
retrieving revision 1.3
diff -u -r1.3 1.cc
--- libstdc++-v3/testsuite/22_locale/codecvt/unshift/wchar_t/1.cc	23 Sep 2003 20:02:16 -0000	1.3
+++ libstdc++-v3/testsuite/22_locale/codecvt/unshift/wchar_t/1.cc	6 May 2005 12:58:38 -0000
@@ -56,7 +56,7 @@
   const w_codecvt* 	cvt = &use_facet<w_codecvt>(loc); 
 
   // unshift
-  strcpy(e_arr, e_lit);
+  memcpy(e_arr, e_lit, size + 1);
   w_codecvt::state_type state03;
   zero_state(state03);
   result r3 = cvt->unshift(state03, e_arr, e_arr + size, eto_next);
Index: libstdc++-v3/testsuite/27_io/basic_streambuf/overflow/char/1.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/testsuite/27_io/basic_streambuf/overflow/char/1.cc,v
retrieving revision 1.3
diff -u -r1.3 1.cc
--- libstdc++-v3/testsuite/27_io/basic_streambuf/overflow/char/1.cc	23 Sep 2003 20:03:18 -0000	1.3
+++ libstdc++-v3/testsuite/27_io/basic_streambuf/overflow/char/1.cc	6 May 2005 12:58:45 -0000
@@ -93,8 +93,7 @@
   typedef testbuf::int_type int_type;
 
   bool test __attribute__((unused)) = true;
-  char lit01[52];
-  strcpy(lit01, "chicago underground trio/possible cube on delmark");
+  char lit01[52] = "chicago underground trio/possible cube on delmark";
   testbuf buf01;
 
   // pbackfail
Index: libstdc++-v3/testsuite/27_io/basic_streambuf/overflow/wchar_t/1.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/testsuite/27_io/basic_streambuf/overflow/wchar_t/1.cc,v
retrieving revision 1.1
diff -u -r1.1 1.cc
--- libstdc++-v3/testsuite/27_io/basic_streambuf/overflow/wchar_t/1.cc	11 Jul 2004 19:13:57 -0000	1.1
+++ libstdc++-v3/testsuite/27_io/basic_streambuf/overflow/wchar_t/1.cc	6 May 2005 12:58:45 -0000
@@ -95,8 +95,7 @@
   typedef testbuf::int_type int_type;
 
   bool test __attribute__((unused)) = true;
-  wchar_t lit01[52];
-  std::wcscpy(lit01, L"chicago underground trio/possible cube on delmark");
+  wchar_t lit01[52] = L"chicago underground trio/possible cube on delmark";
   testbuf buf01;
 
   // pbackfail
Index: libstdc++-v3/testsuite/27_io/basic_streambuf/sgetc/char/1.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/testsuite/27_io/basic_streambuf/sgetc/char/1.cc,v
retrieving revision 1.3
diff -u -r1.3 1.cc
--- libstdc++-v3/testsuite/27_io/basic_streambuf/sgetc/char/1.cc	23 Sep 2003 20:03:18 -0000	1.3
+++ libstdc++-v3/testsuite/27_io/basic_streambuf/sgetc/char/1.cc	6 May 2005 12:58:45 -0000
@@ -93,8 +93,7 @@
   typedef testbuf::int_type int_type;
 
   bool test __attribute__((unused)) = true;
-  char lit01[52];
-  strcpy(lit01, "chicago underground trio/possible cube on delmark");
+  char lit01[52] = "chicago underground trio/possible cube on delmark";
   testbuf buf01;
 
   // 27.5.2.3.1 get area
Index: libstdc++-v3/testsuite/27_io/basic_streambuf/sgetc/wchar_t/1.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/testsuite/27_io/basic_streambuf/sgetc/wchar_t/1.cc,v
retrieving revision 1.1
diff -u -r1.1 1.cc
--- libstdc++-v3/testsuite/27_io/basic_streambuf/sgetc/wchar_t/1.cc	11 Jul 2004 19:13:57 -0000	1.1
+++ libstdc++-v3/testsuite/27_io/basic_streambuf/sgetc/wchar_t/1.cc	6 May 2005 12:58:45 -0000
@@ -95,8 +95,7 @@
   typedef testbuf::int_type int_type;
 
   bool test __attribute__((unused)) = true;
-  wchar_t lit01[52];
-  std::wcscpy(lit01, L"chicago underground trio/possible cube on delmark");
+  wchar_t lit01[52] = L"chicago underground trio/possible cube on delmark";
   testbuf buf01;
 
   // 27.5.2.3.1 get area
Index: libstdc++-v3/testsuite/27_io/basic_streambuf/sgetn/char/1.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/testsuite/27_io/basic_streambuf/sgetn/char/1.cc,v
retrieving revision 1.3
diff -u -r1.3 1.cc
--- libstdc++-v3/testsuite/27_io/basic_streambuf/sgetn/char/1.cc	23 Sep 2003 20:03:19 -0000	1.3
+++ libstdc++-v3/testsuite/27_io/basic_streambuf/sgetn/char/1.cc	6 May 2005 12:58:45 -0000
@@ -94,10 +94,8 @@
 
   bool test __attribute__((unused)) = true;
 
-  const char* lit00 = "chicago underground trio/possible cube on delmark";
-  size_t i01 = traits_type::length(lit00);
-  char lit01[i01];
-  strcpy(lit01, lit00);
+  char lit01[] = "chicago underground trio/possible cube on delmark";
+  size_t i01 = traits_type::length(lit01);
 
   testbuf buf01;
 
Index: libstdc++-v3/testsuite/27_io/basic_streambuf/sgetn/wchar_t/1.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/testsuite/27_io/basic_streambuf/sgetn/wchar_t/1.cc,v
retrieving revision 1.1
diff -u -r1.1 1.cc
--- libstdc++-v3/testsuite/27_io/basic_streambuf/sgetn/wchar_t/1.cc	11 Jul 2004 19:13:57 -0000	1.1
+++ libstdc++-v3/testsuite/27_io/basic_streambuf/sgetn/wchar_t/1.cc	6 May 2005 12:58:45 -0000
@@ -96,10 +96,8 @@
 
   bool test __attribute__((unused)) = true;
 
-  const wchar_t* lit00 = L"chicago underground trio/possible cube on delmark";
-  size_t i01 = traits_type::length(lit00);
-  wchar_t lit01[i01];
-  std::wcscpy(lit01, lit00);
+  wchar_t lit01[] = L"chicago underground trio/possible cube on delmark";
+  size_t i01 = traits_type::length(lit01);
 
   testbuf buf01;
 
Index: libstdc++-v3/testsuite/27_io/basic_streambuf/sputn/char/1.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/testsuite/27_io/basic_streambuf/sputn/char/1.cc,v
retrieving revision 1.3
diff -u -r1.3 1.cc
--- libstdc++-v3/testsuite/27_io/basic_streambuf/sputn/char/1.cc	23 Sep 2003 20:03:19 -0000	1.3
+++ libstdc++-v3/testsuite/27_io/basic_streambuf/sputn/char/1.cc	6 May 2005 12:58:46 -0000
@@ -96,10 +96,8 @@
   testbuf buf01;
 
   // sputn/xsputn
-  const char* lit01 = "isotope 217: the unstable molecule on thrill jockey";
-  const int i02 = std::strlen(lit01);
-  char lit02[i02];
-  std::strcpy(lit02, lit01);
+  char lit02[] = "isotope 217: the unstable molecule on thrill jockey";
+  const int i02 = std::strlen(lit02);
 
   char carray[i02 + 1];
   std::memset(carray, 0, i02 + 1);
Index: libstdc++-v3/testsuite/27_io/basic_streambuf/sputn/wchar_t/1.cc
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/testsuite/27_io/basic_streambuf/sputn/wchar_t/1.cc,v
retrieving revision 1.1
diff -u -r1.1 1.cc
--- libstdc++-v3/testsuite/27_io/basic_streambuf/sputn/wchar_t/1.cc	11 Jul 2004 19:13:58 -0000	1.1
+++ libstdc++-v3/testsuite/27_io/basic_streambuf/sputn/wchar_t/1.cc	6 May 2005 12:58:46 -0000
@@ -98,10 +98,8 @@
   testbuf buf01;
 
   // sputn/xsputn
-  const wchar_t* lit01 = L"isotope 217: the unstable molecule on thrill jockey";
-  const int i02 = std::wcslen(lit01);
-  wchar_t lit02[i02];
-  std::wcscpy(lit02, lit01);
+  wchar_t lit02[] = L"isotope 217: the unstable molecule on thrill jockey";
+  const int i02 = std::wcslen(lit02);
 
   wchar_t carray[i02 + 1];
   std::wmemset(carray, 0, i02 + 1);


More information about the Libstdc++ mailing list