]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2375] Check we don't read past the end
authorMichal 'vorner' Vaner <michal.vaner@nic.cz>
Tue, 13 Nov 2012 18:18:20 +0000 (19:18 +0100)
committerMichal 'vorner' Vaner <michal.vaner@nic.cz>
Mon, 19 Nov 2012 15:54:04 +0000 (16:54 +0100)
src/lib/dns/master_lexer.cc
src/lib/dns/tests/master_lexer_unittest.cc

index 55678335600f237703934443e696adf98391b8dd..4b02bc0f4a156fa8a0d2c5d48868b28c0f892ab0 100644 (file)
@@ -157,7 +157,8 @@ MasterLexer::getNextToken(Options options) {
     // Reset the token now. This is to check a token was actually produced.
     // This is debugging aid.
     impl_->token_ = Token(Token::NO_TOKEN_PRODUCED);
-    if (impl_->source_ == NULL) {
+    // If the source is not available
+    if (impl_->source_ == NULL || impl_->source_->atEOF()) {
         isc_throw(isc::InvalidOperation, "No source to read tokens from");
     }
     for (const State *state = start(options); state != NULL;
@@ -187,7 +188,7 @@ const char* const error_text[] = {
     "unbalanced parentheses",   // UNBALANCED_PAREN
     "unexpected end of input",  // UNEXPECTED_END
     "unbalanced quotes",        // UNBALANCED_QUOTES
-    "no token produced"
+    "no token produced"         // NO_TOKEN_PRODUCED
 };
 const size_t error_text_max_count = sizeof(error_text) / sizeof(error_text[0]);
 }
index 52bc425c368d4cc63de5e06c6ec3fc32d8b67089..a36d0a86793793da16bb8da99468a098d267919f 100644 (file)
@@ -257,4 +257,16 @@ TEST_F(MasterLexerTest, realStart) {
               lexer.getNextToken(MasterLexer::INITIAL_WS).getType());
 }
 
+// Test we correctly find end of file. Then, upon more attempts to produce
+// tokens past the end, it throws.
+TEST_F(MasterLexerTest, eof) {
+    // Let the ss empty.
+    lexer.pushSource(ss);
+
+    // The first one is found to be EOF
+    EXPECT_EQ(MasterLexer::Token::END_OF_FILE, lexer.getNextToken().getType());
+    // And it is not allowed to use this one any more.
+    EXPECT_THROW(lexer.getNextToken(), isc::InvalidOperation);
+}
+
 }