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]

Patch to libstc++: Remove strdup from locale_facets.tcc


This code:

#include <iostream>
void foo() {
  int bar = 1; 
  std::cout << bar << std::endl;
}

fails when compiled with -O3 -ansi because non-ANSI strdup
in locale_facets.tcc is exposed by inlining and strdup is guarded by
__STRICT_ANSI__ in C runtime string.h

g++ -O3 -ansi -c strdup-bug.cpp 
D:/MINGW/include/c++/3.3/bits/locale_facets.tcc: In function `int 
   std::__convert_from_v(char*, int, const char*, _Tv, int* const&, int) [with 
   _Tv = long unsigned int]':
D:/MINGW/include/c++/3.3/bits/locale_facets.tcc:720:   instantiated from
`_OutIter std::num_put<_CharT, _OutIter>::_M_convert_int(_OutIter,
std::ios_base&, _CharT, char, char, _ValueT) const [with _ValueT = long
unsigned int, _CharT = char, _OutIter = std::ostreambuf_iterator<char,
std::char_traits<char> >]'
D:/MINGW/include/c++/3.3/bits/locale_facets.tcc:899:   instantiated from
`_OutIter std::num_put<_CharT, _OutIter>::do_put(_OutIter, std::ios_base&,
_CharT, long unsigned int) const [with _CharT = char, _OutIter =
std::ostreambuf_iterator<char, std::char_traits<char> >]'
D:/MINGW/include/c++/3.3/bits/locale_facets.h:745:   instantiated from
`_OutIter std::num_put<_CharT, _OutIter>::put(_OutIter, std::ios_base&, _CharT,
long unsigned int) const [with _CharT = char, _OutIter =
std::ostreambuf_iterator<char, std::char_traits<char> >]'
D:/MINGW/include/c++/3.3/bits/ostream.tcc:215:   instantiated from
`std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits =
std::char_traits<char>]'
D:/MINGW/include/c++/3.3/ostream:122:   instantiated from
`std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(int) [with _CharT = char, _Traits =
std::char_traits<char>]'
strdup-bug.cpp:6:   instantiated from here
D:/MINGW/include/c++/3.3/bits/locale_facets.tcc:2070: error: `strdup' 
   undeclared (first use this function)
D:/MINGW/include/c++/3.3/bits/locale_facets.tcc:2070: error: (Each undeclared 
   identifier is reported only once for each function it appears in.)


Following patch, tested with i586-pc-mingw32 build of gcc version 3.3 20020907
(experimental) fixes:

ChangeLog

2002-09-10  Danny Smith  <dannysmith@users.sourceforge.net>

	* include/bits/locale_facets.tcc (__convert_from_v):
	Replace strdup with ANSI malloc and strcpy.

Index: locale_facets.tcc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/locale_facets.tcc,v
retrieving revision 1.78
diff -c -3 -p -r1.78 locale_facets.tcc
*** locale_facets.tcc	31 Jul 2002 02:47:33 -0000	1.78
--- locale_facets.tcc	9 Sep 2002 22:58:05 -0000
*************** namespace std
*** 1976,1989 ****
  		     _Tv __v, const __c_locale&, int __prec = -1)
      {
        int __ret;
!       char* __old = strdup(setlocale(LC_ALL, NULL));
        setlocale(LC_ALL, "C");
        if (__prec >= 0)
          __ret = snprintf(__out, __size, __fmt, __prec, __v);
        else
          __ret = snprintf(__out, __size, __fmt, __v);
!       setlocale(LC_ALL, __old);
!       free(__old);
        return __ret;
      }
  #else
--- 1976,1992 ----
  		     _Tv __v, const __c_locale&, int __prec = -1)
      {
        int __ret;
!       char* __old = setlocale(LC_ALL, NULL);
!       char* __sav = static_cast<char*>(malloc(strlen(__old) + 1));
!       if (__sav)
!         strcpy(__sav, __old);
        setlocale(LC_ALL, "C");
        if (__prec >= 0)
          __ret = snprintf(__out, __size, __fmt, __prec, __v);
        else
          __ret = snprintf(__out, __size, __fmt, __v);
!       setlocale(LC_ALL, __sav);
!       free(__sav);
        return __ret;
      }
  #else
*************** namespace std
*** 1993,2006 ****
  		     const __c_locale&, int __prec = -1)
      {
        int __ret;
!       char* __old = strdup(setlocale(LC_ALL, NULL));
        setlocale(LC_ALL, "C");
        if (__prec >= 0)
          __ret = sprintf(__out, __fmt, __prec, __v);
        else
          __ret = sprintf(__out, __fmt, __v);
!       setlocale(LC_ALL, __old);
!       free(__old);
        return __ret;
      }
  #endif
--- 1996,2012 ----
  		     const __c_locale&, int __prec = -1)
      {
        int __ret;
!       char* __old = setlocale(LC_ALL, NULL);
!       char* __sav = static_cast<char*>(malloc(strlen(__old) + 1));
!       if (__sav)
!         strcpy(__sav, __old);
        setlocale(LC_ALL, "C");
        if (__prec >= 0)
          __ret = sprintf(__out, __fmt, __prec, __v);
        else
          __ret = sprintf(__out, __fmt, __v);
!       setlocale(LC_ALL, __sav);
!       free(__sav);
        return __ret;
      }
  #endif

http://mobile.yahoo.com.au - Yahoo! Messenger for SMS
- Now send & receive IMs on your mobile via SMS


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