www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19252] New: Templated format with variable width allocates

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 puremagic.com
          Reporter: default_357-line 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

--
Sep 19 2018