Skip to content

Commit

Permalink
Merge pull request #1997 from tom-tan/trusted-std-stdio-File-error-flush
Browse files Browse the repository at this point in the history
std.File.flush and error causes segfault after calling close
  • Loading branch information
AndrejMitrovic committed Mar 12, 2014
2 parents d362176 + 7bbef15 commit cb26b36
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions std/stdio.d
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

0 comments on commit cb26b36

Please sign in to comment.