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]

[RFC] Changing __convert_from_v to use snprintf


Hi,

today, after having proposed a fix for money_put's risks of buffer oveerruns, it
occurred to me that perhaps there is a cleaner solution to the problem of
allocating large staging buffers.

Indeed, some days ago, I could appreciate the advantage of the third parameter
of strxfrm when working on collate::do_transform: closely following glibc docs
guidelines, first a small buffer is tried, which is replaced with one with
strictly correct size in case of need.

Now I'm asking your opinion on using snprintf (instead of sprintf) in
__convert_from_v, which could allow to deal in a much better way with temporary
buffers in num_put::_M_convert_float, num_put::_M_convert_int,
money_put::do_put, indeed. In short a propose then to use there the very same
scheme of collate::do_transform:

 template<typename _CharT>
    typename collate<_CharT>::string_type
    collate<_CharT>::
    do_transform(const _CharT* __lo, const _CharT* __hi) const
    {
      size_t __len = (__hi - __lo) * 2;
      // First try a buffer perhaps big enough.
      _CharT* __c =
         static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __len));
      size_t __res = _M_transform_helper(__c, __lo, __len);
      // If the buffer was not large enough, try again with the correct size.
      if (__res >= __len)
        {
          _CharT* __c2 =
            static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__res +
1)));
          _M_transform_helper(__c2, __lo, __res + 1);
          return string_type(__c2);
        }
      return string_type(__c);
    }

Opinions? If the idea is liked I can work out a patch in a couple of days...

Thanks,
Paolo.



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