Using strlcpy if target OS allows it

Magnus Fromreide magfr@lysator.liu.se
Fri May 6 04:57:00 GMT 2005


On Mon, May 02, 2005 at 11:18:07PM +0200, Paolo Carlini wrote:
> Hi Magnus,
> 
> >I think the folowing is the patch you were asking for, but see below for
> >further comments.
> >  
> >
> I haven't followed the details of this issue, but I gather that avoidind
> strcpy is
> an improvement from the point of view of secure coding, right?
> 
> In that case, I'm in favor of these, rather limited, anyway, changes.
> 
> 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.

> 2- The size of the arrays in basic_streambuf was simply too small by
> one, no mysteries ;) The fix seems fine.
> 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?

> 4- Finally, remember to always uglify with __ all the variables of the
> implementation.

Ok.

Attached is a new patch.
-------------- next part --------------
Index: libstdc++-v3/ChangeLog
===================================================================
RCS file: /cvsroot/gcc/gcc/libstdc++-v3/ChangeLog,v
retrieving revision 1.2990
diff -u -r1.2990 ChangeLog
--- libstdc++-v3/ChangeLog	5 May 2005 04:05:31 -0000	1.2990
+++ libstdc++-v3/ChangeLog	6 May 2005 04:33:51 -0000
@@ -1,3 +1,22 @@
+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/sgetc/char/1.cc: Likewise
+	* testsuite/27_io/basic_streambuf/sgetn/char/1.cc: Likewise
+
+	* testsuite/27_io/basic_streambuf/sputn/char/1.cc: Use memopy
+	instead of strcpy. Make the target buffer big enough.
+
 2005-05-04  Mark Mitchell  <mark@codesourcery.com>
 
 	* testsuite/lib/libstdc++.exp (v3-build_support): Look for
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 04:33:51 -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 04:33:51 -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 04:33:51 -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 04:33:51 -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 04:33:52 -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 04:33:55 -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 04:33:56 -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 04:34:06 -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/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 04:34:06 -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/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 04:34:06 -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/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 04:34:06 -0000
@@ -98,8 +98,8 @@
   // 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[i02 + 1];
+  std::memcpy(lit02, lit01, i02 + 1);
 
   char carray[i02 + 1];
   std::memset(carray, 0, i02 + 1);


More information about the Libstdc++ mailing list