[Bug libstdc++/87228] New: num_put::_M_insert_float uses stack inefficiently

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Sep 5 11:27:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87228

            Bug ID: 87228
           Summary: num_put::_M_insert_float uses stack inefficiently
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

This function uses alloca to get an initial buffer with a fairly conservative
size, then uses alloca between 1 and 3 more times for the final result.

We do all the work of formatting the string, then right at the end check if we
need to pad to a larger width, and then do another alloca. We know the width
upfront, so could make sure the first alloca has at least that much space.

We should also avoid trying to alloca something ridiculous when a huge width
has been given. This segfaults with a ulimit -s of 8192 kb


#include <iostream>
#include <iomanip>
#include <limits>

int main()
{
  std::cout << std::setw(8192 * 1024 + 1) << std::fixed
    << std::numeric_limits<long double>::max();
}

The function should probably switch to the heap in such cases.


More information about the Gcc-bugs mailing list