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 3363: std.stream.readf segfaults with immutable format strings #2224

Merged
merged 3 commits into from
Jul 11, 2014
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
20 changes: 19 additions & 1 deletion std/stream.d
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,8 @@ class Stream : InputStream, OutputStream {
}
if (fmt.length == 0 || i == fmt.length) {
i = 0;
if (arguments[j] is typeid(char[])) {
if (arguments[j] is typeid(string) || arguments[j] is typeid(char[])
|| arguments[j] is typeid(const(char)[])) {
fmt = va_arg!(string)(args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, this should be va_arg!(const(char)[])(args), and the type of fmt be changed accordingly, but I guess it doesn't really matter because fmt is only used in ways which work regardless of mutability.

j++;
continue;
Expand Down Expand Up @@ -1438,6 +1439,23 @@ class Stream : InputStream, OutputStream {
throw new SeekException("Stream is not seekable");
}

unittest { // unit test for Issue 3363
import std.stdio;
immutable fileName ="issue3363.txt";
auto w = File(fileName, "w");
scope (exit) remove(fileName.ptr);
w.write("one two three");
w.close();
auto r = File(fileName, "r");
const(char)[] constChar;
string str;
char[] chars;
r.readf("%s %s %s", &constChar, &str, &chars);
assert (constChar == "one", constChar);
assert (str == "two", str);
assert (chars == "three", chars);
}

unittest { //unit tests for Issue 1668
void tryFloatRoundtrip(float x, string fmt = "", string pad = "") {
auto s = new MemoryStream();
Expand Down