Skip to content

Commit

Permalink
Fix Issue 15386 - std.format.formatValue usage hangs
Browse files Browse the repository at this point in the history
  • Loading branch information
berni44 authored and dlang-bot committed Mar 22, 2021
1 parent 35b47c2 commit f948001
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions std/format/write.d
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ uint formattedWrite(Writer, Char, A...)(auto ref Writer w, const scope Char[] fm
auto separatorChar =
getNth!("separator character", isSomeChar, dchar)(currentArg, args);
spec.separatorChar = separatorChar;
spec.separatorCharPos = spec.UNSPECIFIED;
++currentArg;
}

Expand Down Expand Up @@ -301,6 +302,12 @@ uint formattedWrite(Writer, Char, A...)(auto ref Writer w, const scope Char[] fm
*/
void formatValue(Writer, T, Char)(auto ref Writer w, auto ref T val, scope const ref FormatSpec!Char f)
{
import std.format : enforceFmt;

enforceFmt(f.width != f.DYNAMIC && f.precision != f.DYNAMIC
&& f.separators != f.DYNAMIC && f.separatorCharPos != f.DYNAMIC,
"Dynamic argument not allowed for `formatValue`");

formatValueImpl(w, val, f);
}

Expand Down Expand Up @@ -615,3 +622,32 @@ void formatValue(Writer, T, Char)(auto ref Writer w, auto ref T val, scope const
assert(to!string(&bar) == "int delegate(short) @nogc delegate() pure nothrow @system");
assert(() @trusted { return bar()(3); }() == 4);
}

// https://issues.dlang.org/show_bug.cgi?id=15386
@safe pure unittest
{
import std.array : appender;
import std.format.spec : FormatSpec;
import std.format : FormatException;
import std.exception : assertThrown;

auto w = appender!(char[])();
auto dor = appender!(char[])();
auto fs = FormatSpec!char("%.*s");
fs.writeUpToNextSpec(dor);
assertThrown!FormatException(formatValue(w, 0, fs));

fs = FormatSpec!char("%*s");
fs.writeUpToNextSpec(dor);
assertThrown!FormatException(formatValue(w, 0, fs));

fs = FormatSpec!char("%,*s");
fs.writeUpToNextSpec(dor);
assertThrown!FormatException(formatValue(w, 0, fs));

fs = FormatSpec!char("%,?s");
fs.writeUpToNextSpec(dor);
assertThrown!FormatException(formatValue(w, 0, fs));

assertThrown!FormatException(formattedWrite(w, "%(%0*d%)", new int[1]));
}

0 comments on commit f948001

Please sign in to comment.