Browse Source

[2506] handle other rare error cases

JINMEI Tatuya 12 years ago
parent
commit
09cd4b9b32
2 changed files with 33 additions and 4 deletions
  1. 7 4
      src/lib/dns/master_lexer.cc
  2. 26 0
      src/lib/dns/tests/master_lexer_unittest.cc

+ 7 - 4
src/lib/dns/master_lexer.cc

@@ -203,19 +203,22 @@ optionsForTokenType(MasterToken::Type expect) {
     case MasterToken::NUMBER:
         return (MasterLexer::NUMBER);
     default:
-        assert(false);
+        isc_throw(InvalidParameter,
+                  "expected type for getNextToken not supported: " << expect);
     }
 }
 }
 
 const MasterToken&
 MasterLexer::getNextToken(MasterToken::Type expect, bool eol_ok) {
-    // the result should be set in impl_->token_
+    // Get the next token, specifying an appropriate option corresponding to
+    // the expected type.  The result should be set in impl_->token_.
     getNextToken(optionsForTokenType(expect));
 
     if (impl_->token_.getType() == MasterToken::ERROR) {
-        //if (impl_->token_.getErrorCode() == MasterToken::NUMBER_OUT_OF_RANGE)
-        ungetToken();
+        if (impl_->token_.getErrorCode() == MasterToken::NUMBER_OUT_OF_RANGE) {
+            ungetToken();
+        }
         throw LexerError(__FILE__, __LINE__, impl_->token_);
     }
 

+ 26 - 0
src/lib/dns/tests/master_lexer_unittest.cc

@@ -398,4 +398,30 @@ TEST_F(MasterLexerTest, getNextTokenNumber) {
     eofCheck(lexer, MasterToken::NUMBER);
 }
 
+TEST_F(MasterLexerTest, getNextTokenErrors) {
+    // Check miscellaneous error cases
+
+    ss << ") ";                 // unbalanced parenthesis
+    ss << "string-after-error ";
+    lexer.pushSource(ss);
+
+    // Only string/qstring/number can be "expected".
+    EXPECT_THROW(lexer.getNextToken(MasterToken::END_OF_LINE),
+                 isc::InvalidParameter);
+    EXPECT_THROW(lexer.getNextToken(MasterToken::END_OF_FILE),
+                 isc::InvalidParameter);
+    EXPECT_THROW(lexer.getNextToken(MasterToken::INITIAL_WS),
+                 isc::InvalidParameter);
+    EXPECT_THROW(lexer.getNextToken(MasterToken::ERROR),
+                 isc::InvalidParameter);
+
+    // If it encounters a syntax error, it results in LexerError exception.
+    lexerErrorCheck(lexer, MasterToken::STRING, MasterToken::UNBALANCED_PAREN);
+
+    // Unlike the NUMBER_OUT_OF_RANGE case, the error part has been skipped
+    // within getNextToken().  We should be able to get the next token.
+    EXPECT_EQ("string-after-error",
+              lexer.getNextToken(MasterToken::STRING).getString());
+}
+
 }