[Issue 19252] New: Templated format with variable width allocates 2GB of RAM per call.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Sep 19 18:56:25 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=19252

          Issue ID: 19252
           Summary: Templated format with variable width allocates 2GB of
                    RAM per call.
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: default_357-line at yahoo.de

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

--


More information about the Digitalmars-d-bugs mailing list