From: Mukund Sivaraman Date: Thu, 1 Nov 2012 05:05:17 +0000 (+0530) Subject: [2369] Distinguish between EOF and failures in getChar() X-Git-Tag: trac2487_base~18^2~16 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=eb92bed532b40a0904f8397ba108d96f152abbbf;p=thirdparty%2Fkea.git [2369] Distinguish between EOF and failures in getChar() --- diff --git a/src/lib/dns/master_lexer_inputsource.cc b/src/lib/dns/master_lexer_inputsource.cc index a871f3c287..a747dfbfc4 100644 --- a/src/lib/dns/master_lexer_inputsource.cc +++ b/src/lib/dns/master_lexer_inputsource.cc @@ -70,10 +70,16 @@ InputSource::getChar() { // Have we reached EOF now? If so, set at_eof_ and return early, // but don't modify buffer_pos_ (which should still be equal to // the size of buffer_). - if (!input_.good()) { + if (input_.eof()) { at_eof_ = true; return (END_OF_STREAM); } + // This has to come after the .eof() check as some + // implementations seem to check the eofbit also in .fail(). + if (input_.fail()) { + isc_throw(ReadError, + "Error reading from the input stream: " << getName()); + } buffer_.push_back(c); } diff --git a/src/lib/dns/master_lexer_inputsource.h b/src/lib/dns/master_lexer_inputsource.h index 46e6147057..aec53950c6 100644 --- a/src/lib/dns/master_lexer_inputsource.h +++ b/src/lib/dns/master_lexer_inputsource.h @@ -79,11 +79,22 @@ public: {} }; + /// \brief Exception thrown when we fail to read from the input + /// stream or file. + struct ReadError : public Unexpected { + ReadError(const char* file, size_t line, const char* what) : + Unexpected(file, line, what) + {} + }; + /// \brief Returned by getChar() when end of stream is reached. static const int END_OF_STREAM; /// \brief Returns a single character from the input source. If end /// of file is reached, \c END_OF_STREAM is returned. + /// + /// \throws ReadError when reading from the input stream or file + /// fails. int getChar(); /// \brief Skips backward a single character in the input