Bug 19252 – Templated format with variable width allocates 2GB of RAM per call.
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-09-19T18:56:25Z
Last change time
2018-10-06T09:38:40Z
Assigned to
No Owner
Creator
FeepingCreature
Comments
Comment #0 by default_357-line — 2018-09-19T18:56:25Z
Consider the following code:
void main()
{
import core.memory;
import std.format;
import std.stdio;
auto s = format!"%0*d"(0, 0);
writefln!"%s, but %s"(s, GC.stats.usedSize);
}
Since 2.079, this will output "Success with output: 0, but 2147483680".
This happens because format will preallocate an appender based on its estimate of the output string length, but guessLength does not know about spec.DYNAMIC (the representation of "*"), which is represented by int.max. So ... guessLength guesses a length of int.max plus some small fry, allocating 2GB.
This does not explode on Linux because the GC uses mmap, and Linux happily lets the program overcommit and allocate terabytes of RAM, since it isn't going to be used. But it's still quite bad.
Fixed by https://github.com/dlang/phobos/pull/6713
Comment #1 by github-bugzilla — 2018-10-06T09:38:37Z