From ae5ad8a49e81f94e2fd939ace67ba0f4f4aca96b Mon Sep 17 00:00:00 2001 From: Jeroen Date: Mon, 7 Mar 2016 18:55:18 +0100 Subject: [PATCH] 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. --- moses/LVoc.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/moses/LVoc.h b/moses/LVoc.h index 47ecbc439..c81ccb7cf 100644 --- a/moses/LVoc.h +++ b/moses/LVoc.h @@ -58,7 +58,14 @@ public: void Write(const std::string& fname) const { std::ofstream out(fname.c_str()); + // Little-known fact: ofstream tracks failures but does not, by default, + // report them. You have to tell it to, or check for errors yourself. + out.exceptions(std::ifstream::failbit | std::ifstream::badbit); Write(out); + // Make sure the file is flushed, so that any errors are reported. If we + // flush implicitly in the destructor, it won't be able to throw + // exceptions. + out.close(); } void Write(std::ostream& out) const { for(int i=data.size()-1; i>=0; --i)