]> gcc.gnu.org Git - gcc.git/commitdiff
re PR libstdc++/24450 (Exception safety problem in messages/__timepunct)
authorPaolo Carlini <pcarlini@suse.de>
Fri, 21 Oct 2005 08:34:06 +0000 (08:34 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Fri, 21 Oct 2005 08:34:06 +0000 (08:34 +0000)
2005-10-21  Paolo Carlini  <pcarlini@suse.de>

PR libstdc++/24450
* config/locale/generic/time_members.h (__timepunct<>::
__timepunct(__c_locale, const char*, size_t)): Avoid leaking
memory if new throws inside _M_initialize_timepunct.
* config/locale/gnu/time_members.h (__timepunct<>::
__timepunct(__c_locale, const char*, size_t)): Likewise.
* config/locale/gnu/message_members.h (messages<>::
messages(__c_locale, const char*, size_t)): Rearrange to
avoid memory leaks.

From-SVN: r105729

libstdc++-v3/ChangeLog
libstdc++-v3/config/locale/generic/time_members.h
libstdc++-v3/config/locale/gnu/messages_members.h
libstdc++-v3/config/locale/gnu/time_members.h

index 902f5f27dafdaad957408cc87d78b2ddda64f3fe..0bc4a95e8b4377134acace3fda5b788e1adf3477 100644 (file)
@@ -1,3 +1,15 @@
+2005-10-21  Paolo Carlini  <pcarlini@suse.de>
+
+       PR libstdc++/24450
+       * config/locale/generic/time_members.h (__timepunct<>::
+       __timepunct(__c_locale, const char*, size_t)): Avoid leaking
+       memory if new throws inside _M_initialize_timepunct.
+       * config/locale/gnu/time_members.h (__timepunct<>::
+       __timepunct(__c_locale, const char*, size_t)): Likewise.
+       * config/locale/gnu/message_members.h (messages<>::
+       messages(__c_locale, const char*, size_t)): Rearrange to
+       avoid memory leaks.
+
 2005-10-19  Paolo Carlini  <pcarlini@suse.de>
 
        * include/ext/sso_string_base.h (_M_swap): Rewrite.
index deea0c8feddb76c1a51c0f6d7180cff1d80d6d20..b3112dc4e37d5d9a958f89d812e322ad87f56848 100644 (file)
@@ -1,6 +1,6 @@
 // std::time_get, std::time_put implementation, generic version -*- C++ -*-
 
-// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
+// Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
                                     size_t __refs) 
     : facet(__refs), _M_data(NULL)
     { 
-      char* __tmp = new char[std::strlen(__s) + 1];
-      std::strcpy(__tmp, __s);
+      const size_t __len = std::strlen(__s) + 1;
+      char* __tmp = new char[__len];
+      std::memcpy(__tmp, __s, __len);
       _M_name_timepunct = __tmp;
-      _M_initialize_timepunct(__cloc); 
+
+      try
+       { _M_initialize_timepunct(__cloc); }
+      catch(...)
+       { 
+         delete [] _M_name_timepunct;
+         __throw_exception_again;
+       }
     }
 
   template<typename _CharT>
index 92081e3e376e40e3f6fd814766557109751c0486..5fb7a999da016af95e36334c4a74c3cac60dab25 100644 (file)
@@ -1,6 +1,6 @@
 // std::messages implementation details, GNU version -*- C++ -*-
 
-// Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+// Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
   template<typename _CharT>
      messages<_CharT>::messages(size_t __refs)
      : facet(__refs), _M_c_locale_messages(_S_get_c_locale()), 
-     _M_name_messages(_S_get_c_name())
+       _M_name_messages(_S_get_c_name())
      { }
 
   template<typename _CharT>
      messages<_CharT>::messages(__c_locale __cloc, const char* __s, 
                                size_t __refs) 
-     : facet(__refs), _M_c_locale_messages(_S_clone_c_locale(__cloc)),
-     _M_name_messages(__s)
+     : facet(__refs), _M_c_locale_messages(NULL), _M_name_messages(NULL)
      {
-       char* __tmp = new char[std::strlen(__s) + 1];
-       std::strcpy(__tmp, __s);
+       const size_t __len = std::strlen(__s) + 1;
+       char* __tmp = new char[__len];
+       std::memcpy(__tmp, __s, __len);
        _M_name_messages = __tmp;
+
+       // Last to avoid leaking memory if new throws.
+       _M_c_locale_messages = _S_clone_c_locale(__cloc);
      }
 
   template<typename _CharT>
index 37a01ef4793c4212a70e2a009b423496cbfaf42e..7014630731e4757d1a38dc374aac4a76615fd641 100644 (file)
@@ -1,6 +1,6 @@
 // std::time_get, std::time_put implementation, GNU version -*- C++ -*-
 
-// Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+// Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
   template<typename _CharT>
     __timepunct<_CharT>::__timepunct(size_t __refs) 
     : facet(__refs), _M_data(NULL), _M_c_locale_timepunct(NULL), 
-    _M_name_timepunct(_S_get_c_name())
+      _M_name_timepunct(_S_get_c_name())
     { _M_initialize_timepunct(); }
 
   template<typename _CharT>
     __timepunct<_CharT>::__timepunct(__cache_type* __cache, size_t __refs) 
     : facet(__refs), _M_data(__cache), _M_c_locale_timepunct(NULL), 
-    _M_name_timepunct(_S_get_c_name())
+      _M_name_timepunct(_S_get_c_name())
     { _M_initialize_timepunct(); }
 
   template<typename _CharT>
     __timepunct<_CharT>::__timepunct(__c_locale __cloc, const char* __s,
                                     size_t __refs) 
     : facet(__refs), _M_data(NULL), _M_c_locale_timepunct(NULL), 
-    _M_name_timepunct(__s)
+      _M_name_timepunct(NULL)
     { 
-      char* __tmp = new char[std::strlen(__s) + 1];
-      std::strcpy(__tmp, __s);
+      const size_t __len = std::strlen(__s) + 1;
+      char* __tmp = new char[__len];
+      std::memcpy(__tmp, __s, __len);
       _M_name_timepunct = __tmp;
-      _M_initialize_timepunct(__cloc); 
+
+      try
+       { _M_initialize_timepunct(__cloc); }
+      catch(...)
+       {
+         delete [] _M_name_timepunct;
+         __throw_exception_again;
+       }
     }
 
   template<typename _CharT>
This page took 0.073483 seconds and 5 git commands to generate.