On 05/09/18 15:32 +0100, Jonathan Wakely wrote:
Using a huge width in a formatted output operation results in stack overflow due to no restriction on the size passed to alloca. This patch causes the formatting functions to use the heap instead of the stack when a large buffer is needed.PR libstdc++/87228 * include/bits/locale_facets.tcc (num_put:_M_insert_int): Use heap for large buffers instead of alloca. (num_put:_M_insert_float): Likewise. * testsuite/22_locale/num_put/put/char/87228.cc: New test. * testsuite/22_locale/num_put/put/wchar_t/87228.cc: New test. Tested x86_64-linux. Even with this patch we can still put 3kb on the stack, but that's much better than trying (and failing) to use alloca for huge values. An alternative would be to just check for silly values and throw std::length_error, but I think this is the right fix. I'll wait a day or two for any comments or better ideas.
Unlimited alloca use is the subject of PR 28277. In the comments Andrew Pinski said "If there are checks then it will be slow." I think in the specific case of num_put::do_put we're already dealing with iostreams and locales, so a few extra branches isn't going to hurt much. I think it's certainly better than a segfault.
