This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ 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]

Re: [PATCH, RFA] Reworked fix for PR4402


Well, I have just noticed that the additional parameter is not needed at all, since __len is
already there for exactly the same purpose! The patch is much simpler.

Cheers,

Paolo Carlini <pcarlini@unitus.it>

        libstdc++/4402
        * testsuite/27_io/ostream_inserter_arith.cc (test02): add testcase from the PR.
        * include/bits/locale_facets.tcc (num_put::_M_convert_float):
        Deal properly with long ios_base::fixed floats.
        * include/bits/locale_facets.tcc (num_put::_M_widen_float): use __len in
        __builtin_alloca call.

diff -urN gcc-vanilla/libstdc++-v3/include/bits/locale_facets.tcc
gcc/libstdc++-v3/include/bits/locale_facets.tcc
--- gcc-vanilla/libstdc++-v3/include/bits/locale_facets.tcc Thu Nov 29 23:59:37 2001
+++ gcc/libstdc++-v3/include/bits/locale_facets.tcc Sat Dec  1 21:30:28 2001
@@ -716,10 +716,17 @@
  // Protect against sprintf() buffer overflows.
  if (__prec > __max_prec)
    __prec = __max_prec;
-
  // Long enough for the max format spec.
  char __fbuf[16];
- char __cs[64];
+
+ // Consider the possibility of long ios_base::fixed outputs
+ const bool __long_fixed =
+   ((__io.flags() & ios_base::fixed) && (__v < -1e35 || __v > 1e35));
+ const int __cs_size = __long_fixed ?
+   numeric_limits<_ValueT>::max_exponent10 + __max_prec + 4
+   : __max_prec*4;
+ char* __cs = static_cast<char*>(__builtin_alloca(sizeof(char) * __cs_size));
+
  int __len;
  // [22.2.2.2.2] Stage 1, numeric conversion to character.
  if (_S_format_float(__io, __fbuf, __mod, __prec))
@@ -757,9 +764,13 @@
       // numpunct.decimal_point() values for '.' and adding grouping.
       const locale __loc = __io.getloc();
       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
-      _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * 64));
+      // Grouping needs typically ~1/3 additional chars, but this is
+      // only a first approximation. More work is needed here to accurately
+      // estimate the minimal safe size for __ws...
+      _CharT* __ws =
+        static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __len * 4 / 3));
       __ctype.widen(__cs, __cs + __len, __ws);
-
+
       const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
       // Replace decimal point.
       const _CharT* __p;
diff -urN gcc-vanilla/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc
gcc/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc
--- gcc-vanilla/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc Thu Nov 29 23:59:38
2001
+++ gcc/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc Sat Dec  1 11:25:07 2001
@@ -272,6 +272,22 @@
 #endif
   VERIFY(os && os.str() == largebuf);

+  // make sure we can output a long float in fixed format
+  // without seg-faulting (libstdc++/4402)
+  double val2 = 3.5e230;
+
+  ostringstream os2;
+  os2.precision(3);
+  os2.setf(ios::fixed);
+  os2 << val2;
+
+  sprintf(largebuf, "%.*f", 3, val2);
+#ifdef TEST_NUMPUT_VERBOSE
+  cout << "expect: " << largebuf << endl;
+  cout << "result: " << os2.str() << endl;
+#endif
+  VERIFY(os2 && os2.str() == largebuf);
+
   return 0;
 }




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