The set of 'internal' functions in std.format called "formatValue(...)" are not private, so assuming users should be allowed to call them, consider the following code:
import std.array, std.format;
auto w = appender!(Char[])();
auto fs = FormatSpec!Char("%.*s");
fs.writeUpToNextSpec(dor); // dor = DummyOutputRange that eats everything
formatValue(w, 0, fs);
The '*' in the format string means an additional argument would specify the precision in this case. When calling std.format.format() or std.format.formattedWrite() it will usually complain about 'expected int' or 'Range violation' which seems okay - at least it's better than what the code above does, which appears to hang the program indefinitely.
If the above code is just bad usage and I should use the formattedWrite interface, then should the other components of std.format be private?
On a similar note: is the format string happens to be compile-time constant, as it is in this case, shouldn't it be possible to check the variadic arguments against the string and find the error at compile time? I suppose it would need to be a template parameter string.
Comment #1 by Tachyon165 — 2015-11-29T06:24:17Z
Actually, letting it run longer reveals that it is not hanging but consuming over 4Gb of memory as it is generating a string of approx 2^32 '0's. lol.
I think this is using the placeholder value defined in FormatSpec for a dynamically specified precision (or width) which is near int.max.
Maybe this is the intended behavior in this (probably incorrect) use case, but it is concerning since format strings are runtime values that can change -- simply putting a '*' in there can allocate 4Gb of memory and/or cause an OutOfMemory error.
Comment #2 by r.97all — 2016-10-18T23:08:43Z
Not only the 'internal' ones, but also from public functions, this issue can be reproduced:
void main()
{
import std.stdio;
"%(%0*d%)".writefln(new int[1]);
}
I intended to specify equal width for each element of an array, put the width before new int[1] and got a runtime error. Removing the width from the arguments, I've come up with this code.