Browse Source

[2369] Distinguish between EOF and failures in getChar()

Mukund Sivaraman 12 years ago
parent
commit
eb92bed532
2 changed files with 18 additions and 1 deletions
  1. 7 1
      src/lib/dns/master_lexer_inputsource.cc
  2. 11 0
      src/lib/dns/master_lexer_inputsource.h

+ 7 - 1
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);
     }
 

+ 11 - 0
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