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

std.File.flush and error causes segfault after calling close #1997

Merged
3 commits merged into from
Mar 12, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions std/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,21 @@ If the file is not opened, returns $(D false). Otherwise, returns
$(WEB cplusplus.com/reference/clibrary/cstdio/ferror.html, ferror) for
the file handle.
*/
@property bool error() const pure nothrow
@property bool error() const @trusted pure nothrow
{
return !_p.handle || .ferror(cast(FILE*) _p.handle);
return !isOpen || .ferror(cast(FILE*) _p.handle);
}

unittest
{
// Issue 12349
static import std.file;
auto deleteme = testFilename();
auto f = File(deleteme, "w");
scope(exit) std.file.remove(deleteme);

f.close();
assert(f.error);
}

/**
Expand Down Expand Up @@ -634,13 +646,26 @@ for the file handle.

Throws: $(D Exception) if the file is not opened or if the call to $(D fflush) fails.
*/
void flush()
void flush() @trusted
{
import std.exception : enforce, errnoEnforce;

errnoEnforce
(.fflush(enforce(_p.handle, "Calling fflush() on an unopened file"))
== 0);
enforce(isOpen, "Attempting to flush() in an unopened file");
errnoEnforce(.fflush(_p.handle) == 0);
}

unittest
{
// Issue 12349
static import std.file;
import std.exception: assertThrown;

auto deleteme = testFilename();
auto f = File(deleteme, "w");
scope(exit) std.file.remove(deleteme);

f.close();
assertThrown(f.flush());
}

/**
Expand Down