Skip to content

Commit ae5ad8a

Browse files
author
Jeroen
committed
Detect write errors in LVoc.
Write failures in LVoc::Write() were going unnoticed. If disk space runs out, the output file might get truncated without any indication of failure.
1 parent c6314d9 commit ae5ad8a

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

moses/LVoc.h

+7
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ class LVoc
5858

5959
void Write(const std::string& fname) const {
6060
std::ofstream out(fname.c_str());
61+
// Little-known fact: ofstream tracks failures but does not, by default,
62+
// report them. You have to tell it to, or check for errors yourself.
63+
out.exceptions(std::ifstream::failbit | std::ifstream::badbit);
6164
Write(out);
65+
// Make sure the file is flushed, so that any errors are reported. If we
66+
// flush implicitly in the destructor, it won't be able to throw
67+
// exceptions.
68+
out.close();
6269
}
6370
void Write(std::ostream& out) const {
6471
for(int i=data.size()-1; i>=0; --i)

0 commit comments

Comments
 (0)