diff options
author | Jeroen Vermeulen <jtv@precisiontranslationtools.com> | 2015-06-13 17:31:53 +0300 |
---|---|---|
committer | Jeroen Vermeulen <jtv@precisiontranslationtools.com> | 2015-06-13 17:31:53 +0300 |
commit | bd86ceffbe6d748a863e15def2443150ca360b38 (patch) | |
tree | f9d6df59636c26a0584d7182271621614668ce9b | |
parent | 166bf7365f02cf573265d7aded822f0d08215de0 (diff) |
Check for error when opening gzfilebuf.
This replaces a segfault when a file can't be found with an exception.
Not as helpful as it could be yet, but certainly better than just
crashing.
Also, make InputFileStream constructor from path "explicit" to avoid
mistakes.
-rw-r--r-- | moses/InputFileStream.h | 2 | ||||
-rw-r--r-- | moses/gzfilebuf.h | 3 | ||||
-rw-r--r-- | phrase-extract/InputFileStream.h | 2 | ||||
-rw-r--r-- | phrase-extract/extract-mixed-syntax/InputFileStream.h | 2 | ||||
-rw-r--r-- | phrase-extract/extract-mixed-syntax/gzfilebuf.h | 4 | ||||
-rw-r--r-- | phrase-extract/gzfilebuf.h | 4 | ||||
-rwxr-xr-x | phrase-extract/lexical-reordering/InputFileStream.h | 2 | ||||
-rwxr-xr-x | phrase-extract/lexical-reordering/gzfilebuf.h | 4 |
8 files changed, 16 insertions, 7 deletions
diff --git a/moses/InputFileStream.h b/moses/InputFileStream.h index d53abfc23..313ddfed7 100644 --- a/moses/InputFileStream.h +++ b/moses/InputFileStream.h @@ -37,7 +37,7 @@ protected: std::streambuf *m_streambuf; public: - InputFileStream(const std::string &filePath); + explicit InputFileStream(const std::string &filePath); ~InputFileStream(); void Close(); diff --git a/moses/gzfilebuf.h b/moses/gzfilebuf.h index 2376c2875..c82092933 100644 --- a/moses/gzfilebuf.h +++ b/moses/gzfilebuf.h @@ -1,6 +1,7 @@ #ifndef moses_gzfile_buf_h #define moses_gzfile_buf_h +#include <stdexcept> #include <streambuf> #include <zlib.h> #include <cstring> @@ -13,6 +14,8 @@ class gzfilebuf : public std::streambuf public: gzfilebuf(const char *filename) { _gzf = gzopen(filename, "rb"); + if (!_gzf) + throw std::runtime_error("Could not open " + std::string(filename) + "."); setg (_buff+sizeof(int), // beginning of putback area _buff+sizeof(int), // read position _buff+sizeof(int)); // end position diff --git a/phrase-extract/InputFileStream.h b/phrase-extract/InputFileStream.h index e2a31bc82..5de416237 100644 --- a/phrase-extract/InputFileStream.h +++ b/phrase-extract/InputFileStream.h @@ -37,7 +37,7 @@ protected: std::streambuf *m_streambuf; public: - InputFileStream(const std::string &filePath); + explicit InputFileStream(const std::string &filePath); ~InputFileStream(); void Close(); diff --git a/phrase-extract/extract-mixed-syntax/InputFileStream.h b/phrase-extract/extract-mixed-syntax/InputFileStream.h index e2a31bc82..5de416237 100644 --- a/phrase-extract/extract-mixed-syntax/InputFileStream.h +++ b/phrase-extract/extract-mixed-syntax/InputFileStream.h @@ -37,7 +37,7 @@ protected: std::streambuf *m_streambuf; public: - InputFileStream(const std::string &filePath); + explicit InputFileStream(const std::string &filePath); ~InputFileStream(); void Close(); diff --git a/phrase-extract/extract-mixed-syntax/gzfilebuf.h b/phrase-extract/extract-mixed-syntax/gzfilebuf.h index b5b0ce87f..4c818ddbb 100644 --- a/phrase-extract/extract-mixed-syntax/gzfilebuf.h +++ b/phrase-extract/extract-mixed-syntax/gzfilebuf.h @@ -1,6 +1,7 @@ #ifndef moses_gzfile_buf_h #define moses_gzfile_buf_h +#include <stdexcept> #include <streambuf> #include <zlib.h> #include <cstring> @@ -10,7 +11,8 @@ class gzfilebuf : public std::streambuf public: gzfilebuf(const char *filename) { _gzf = gzopen(filename, "rb"); - setg (_buff+sizeof(int), // beginning of putback area + if (!_gzf) + throw std::runtime_error("Could not open " + std::string(filename) + "."); setg (_buff+sizeof(int), // beginning of putback area _buff+sizeof(int), // read position _buff+sizeof(int)); // end position } diff --git a/phrase-extract/gzfilebuf.h b/phrase-extract/gzfilebuf.h index b5b0ce87f..4c818ddbb 100644 --- a/phrase-extract/gzfilebuf.h +++ b/phrase-extract/gzfilebuf.h @@ -1,6 +1,7 @@ #ifndef moses_gzfile_buf_h #define moses_gzfile_buf_h +#include <stdexcept> #include <streambuf> #include <zlib.h> #include <cstring> @@ -10,7 +11,8 @@ class gzfilebuf : public std::streambuf public: gzfilebuf(const char *filename) { _gzf = gzopen(filename, "rb"); - setg (_buff+sizeof(int), // beginning of putback area + if (!_gzf) + throw std::runtime_error("Could not open " + std::string(filename) + "."); setg (_buff+sizeof(int), // beginning of putback area _buff+sizeof(int), // read position _buff+sizeof(int)); // end position } diff --git a/phrase-extract/lexical-reordering/InputFileStream.h b/phrase-extract/lexical-reordering/InputFileStream.h index 1f37715fd..dcc28a60c 100755 --- a/phrase-extract/lexical-reordering/InputFileStream.h +++ b/phrase-extract/lexical-reordering/InputFileStream.h @@ -37,7 +37,7 @@ protected: std::streambuf *m_streambuf; public: - InputFileStream(const std::string &filePath); + explicit InputFileStream(const std::string &filePath); ~InputFileStream(); void Open(const std::string &filePath); diff --git a/phrase-extract/lexical-reordering/gzfilebuf.h b/phrase-extract/lexical-reordering/gzfilebuf.h index b5b0ce87f..4c818ddbb 100755 --- a/phrase-extract/lexical-reordering/gzfilebuf.h +++ b/phrase-extract/lexical-reordering/gzfilebuf.h @@ -1,6 +1,7 @@ #ifndef moses_gzfile_buf_h #define moses_gzfile_buf_h +#include <stdexcept> #include <streambuf> #include <zlib.h> #include <cstring> @@ -10,7 +11,8 @@ class gzfilebuf : public std::streambuf public: gzfilebuf(const char *filename) { _gzf = gzopen(filename, "rb"); - setg (_buff+sizeof(int), // beginning of putback area + if (!_gzf) + throw std::runtime_error("Could not open " + std::string(filename) + "."); setg (_buff+sizeof(int), // beginning of putback area _buff+sizeof(int), // read position _buff+sizeof(int)); // end position } |