Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 15386 - std.format.formatValue usage hangs #7898

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]));
}