This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] Fix locale const correctness bug


Hi,

as pointed out on the libstdc++ list by Sebor.

Tested x86-linux, committed to trunk.

Paolo.

///////////
2003-02-01  Paolo Carlini  <pcarlini@unitus.it>
	    Benjamin Kosnik  <bkoz@redhat.com>

	Const correctness issue:
	http://gcc.gnu.org/ml/libstdc++/2003-01/msg00370.html
	* include/bits/locale_classes.h
	(locale::_Impl::_M_facets): Change type to const facet**.
	(locale::_Impl::_M_install_facet): Change declaration to
	take const facet*.
	(locale::facet::_M_references): Make mutable.
	(locale::facet::_M_add_reference): Declare const.
	(locale::facet::_M_remove_reference): Likewise.
	* include/bits/locale_facets.tcc
	(use_facet(const locale&)): Tweak for const facet** _M_facets.
	(has_facet(const locale&)): Likewise.
	* src/locale.cc
	(locale::facet::_M_add_reference): Adjust definition.
	(locale::facet::_M_remove_reference): Likewise.
	* src/localename.cc
	(locale::_Impl::_Impl(const _Impl&, size_t)): Tweak for
	const facet** _M_facets.
	(locale::_Impl::_Impl(const char*, size_t)): Likewise.
	(locale::_Impl::_Impl(facet**, size_t, bool)): Likewise.
	(locale::_Impl::_M_install_facet): Adjust definition to take
	const facet* and for const facet** _M_facets.
	* testsuite/22_locale/locale/cons/8.cc: Add.
diff -urN libstdc++-v3-orig/include/bits/locale_classes.h libstdc++-v3/include/bits/locale_classes.h
--- libstdc++-v3-orig/include/bits/locale_classes.h	2003-01-23 19:56:00.000000000 +0100
+++ libstdc++-v3/include/bits/locale_classes.h	2003-01-31 22:21:50.000000000 +0100
@@ -199,7 +199,7 @@
   private:
     // Data Members.
     _Atomic_word			_M_references;
-    facet** 				_M_facets;
+    const facet**			_M_facets;
     size_t 				_M_facets_size;
 
     char* 				_M_names[_S_categories_size
@@ -260,7 +260,7 @@
     _M_replace_facet(const _Impl*, const locale::id*);
 
     void 
-    _M_install_facet(const locale::id*, facet*);
+    _M_install_facet(const locale::id*, const facet*);
 
     template<typename _Facet>
       inline void 
@@ -291,7 +291,7 @@
     friend class locale;
     friend class locale::_Impl;
 
-    _Atomic_word 			_M_references;
+    mutable _Atomic_word		_M_references;
 
   protected:
     // Contains data from the underlying "C" library for the classic locale.
@@ -318,10 +318,10 @@
 
   private:
     void 
-    _M_add_reference() throw();
+    _M_add_reference() const throw();
 
     void 
-    _M_remove_reference() throw();
+    _M_remove_reference() const throw();
 
     facet(const facet&);  // Not defined.
 
diff -urN libstdc++-v3-orig/include/bits/locale_facets.tcc libstdc++-v3/include/bits/locale_facets.tcc
--- libstdc++-v3-orig/include/bits/locale_facets.tcc	2003-01-22 17:51:51.000000000 +0100
+++ libstdc++-v3/include/bits/locale_facets.tcc	2003-01-31 21:54:14.000000000 +0100
@@ -71,7 +71,7 @@
     use_facet(const locale& __loc)
     {
       size_t __i = _Facet::id._M_id();
-      locale::facet** __facets = __loc._M_impl->_M_facets;
+      const locale::facet** __facets = __loc._M_impl->_M_facets;
       if (!(__i < __loc._M_impl->_M_facets_size && __facets[__i]))
         __throw_bad_cast();
       return static_cast<const _Facet&>(*__facets[__i]);
@@ -82,7 +82,7 @@
     has_facet(const locale& __loc) throw()
     {
       size_t __i = _Facet::id._M_id();
-      locale::facet** __facets = __loc._M_impl->_M_facets;
+      const locale::facet** __facets = __loc._M_impl->_M_facets;
       return (__i < __loc._M_impl->_M_facets_size && __facets[__i]);
     }
 
diff -urN libstdc++-v3-orig/src/locale.cc libstdc++-v3/src/locale.cc
--- libstdc++-v3-orig/src/locale.cc	2003-01-23 18:21:11.000000000 +0100
+++ libstdc++-v3/src/locale.cc	2003-01-31 21:47:48.000000000 +0100
@@ -461,12 +461,12 @@
 
   void  
   locale::facet::
-  _M_add_reference() throw()
+  _M_add_reference() const throw()
   { __atomic_add(&_M_references, 1); }
 
   void  
   locale::facet::
-  _M_remove_reference() throw()
+  _M_remove_reference() const throw()
   {
     if (__exchange_and_add(&_M_references, -1) == 1)
       {
diff -urN libstdc++-v3-orig/src/localename.cc libstdc++-v3/src/localename.cc
--- libstdc++-v3-orig/src/localename.cc	2003-01-06 22:22:59.000000000 +0100
+++ libstdc++-v3/src/localename.cc	2003-01-31 21:56:25.000000000 +0100
@@ -94,7 +94,7 @@
   {
     try
       { 
-	_M_facets = new facet*[_M_facets_size]; 
+	_M_facets = new const facet*[_M_facets_size]; 
 	for (size_t __i = 0; __i < _M_facets_size; ++__i)
 	  _M_facets[__i] = 0;
       }
@@ -130,7 +130,7 @@
 
     try
       { 
-	_M_facets = new facet*[_M_facets_size]; 
+	_M_facets = new const facet*[_M_facets_size]; 
 	for (size_t __i = 0; __i < _M_facets_size; ++__i)
 	  _M_facets[__i] = 0;
       }
@@ -214,7 +214,7 @@
     locale::facet::_S_create_c_locale(locale::facet::_S_c_locale, 
 				      locale::facet::_S_c_name);
 
-    _M_facets = new(&facet_vec) facet*[_M_facets_size];
+    _M_facets = new(&facet_vec) const facet*[_M_facets_size];
     for (size_t __i = 0; __i < _M_facets_size; ++__i)
       _M_facets[__i] = 0;
 
@@ -310,7 +310,7 @@
 
   void
   locale::_Impl::
-  _M_install_facet(const locale::id* __idp, facet* __fp)
+  _M_install_facet(const locale::id* __idp, const facet* __fp)
   {
     if (__fp)
       {
@@ -319,10 +319,10 @@
 	// Check size of facet vector to ensure adequate room.
 	if (__index > _M_facets_size - 1)
 	  {
-	    facet** __old = _M_facets;
-	    facet** __new;
+	    const facet** __old = _M_facets;
+	    const facet** __new;
 	    const size_t __new_size = __index + 4;
-	    __new = new facet*[__new_size]; 
+	    __new = new const facet*[__new_size]; 
 	    for (size_t __i = 0; __i < _M_facets_size; ++__i)
 	      __new[__i] = _M_facets[__i];
 	    for (size_t __i2 = _M_facets_size; __i2 < __new_size; ++__i2)
@@ -334,7 +334,7 @@
 	  }
 
 	__fp->_M_add_reference();
-	facet*& __fpr = _M_facets[__index];
+	const facet*& __fpr = _M_facets[__index];
 	if (__fpr)
 	  {
 	    // Replacing an existing facet. Order matters.
diff -urN libstdc++-v3-orig/testsuite/22_locale/locale/cons/8.cc libstdc++-v3/testsuite/22_locale/locale/cons/8.cc
--- libstdc++-v3-orig/testsuite/22_locale/locale/cons/8.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/22_locale/locale/cons/8.cc	2003-01-31 22:31:42.000000000 +0100
@@ -0,0 +1,43 @@
+// 2003-02-01 Paolo Carlini <pcarlini@unitus.it>
+
+// Copyright (C) 2003 Free Software Foundation
+//
+// 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
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 22.1.1.2 locale constructors and destructors [lib.locale.cons]
+
+#include <locale>
+
+// Const correctness issue:
+// http://gcc.gnu.org/ml/libstdc++/2003-01/msg00370.html
+void
+test01()
+{
+  using namespace std;
+  bool test = true;
+
+  const locale l1("C");
+  const locale l2 =
+    locale(locale::classic(), &use_facet<time_get<char> >(l1));
+}
+
+
+int main()
+{
+  test01();
+  return 0;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]