From: Mehdi Amini Date: Mon, 28 Nov 2016 21:38:52 +0000 (+0000) Subject: Improve error handling in YAML parsing X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b54cba4a12b0dfb073a115ffeea6e7324bf46814;p=llvm Improve error handling in YAML parsing Some scanner errors were not checked and reported by the parser. Fix PR30934. Recommit r288014 after fixing unittest. Patch by: Serge Guelton Differential Revision: https://reviews.llvm.org/D26419 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288071 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Support/YAMLParser.h b/include/llvm/Support/YAMLParser.h index dad6740ee06..b9e3fa47752 100644 --- a/include/llvm/Support/YAMLParser.h +++ b/include/llvm/Support/YAMLParser.h @@ -42,6 +42,7 @@ #include "llvm/Support/Allocator.h" #include "llvm/Support/SMLoc.h" #include +#include #include namespace llvm { @@ -75,9 +76,11 @@ std::string escape(StringRef Input); class Stream { public: /// \brief This keeps a reference to the string referenced by \p Input. - Stream(StringRef Input, SourceMgr &, bool ShowColors = true); + Stream(StringRef Input, SourceMgr &, bool ShowColors = true, + std::error_code *EC = nullptr); - Stream(MemoryBufferRef InputBuffer, SourceMgr &, bool ShowColors = true); + Stream(MemoryBufferRef InputBuffer, SourceMgr &, bool ShowColors = true, + std::error_code *EC = nullptr); ~Stream(); document_iterator begin(); diff --git a/lib/Support/YAMLParser.cpp b/lib/Support/YAMLParser.cpp index b74ea351c90..c17a6f6e1ea 100644 --- a/lib/Support/YAMLParser.cpp +++ b/lib/Support/YAMLParser.cpp @@ -232,8 +232,10 @@ namespace yaml { /// @brief Scans YAML tokens from a MemoryBuffer. class Scanner { public: - Scanner(StringRef Input, SourceMgr &SM, bool ShowColors = true); - Scanner(MemoryBufferRef Buffer, SourceMgr &SM_, bool ShowColors = true); + Scanner(StringRef Input, SourceMgr &SM, bool ShowColors = true, + std::error_code *EC = nullptr); + Scanner(MemoryBufferRef Buffer, SourceMgr &SM_, bool ShowColors = true, + std::error_code *EC = nullptr); /// @brief Parse the next token and return it without popping it. Token &peekNext(); @@ -250,6 +252,10 @@ public: if (Current >= End) Current = End - 1; + // propagate the error if possible + if (EC) + *EC = make_error_code(std::errc::invalid_argument); + // Don't print out more errors after the first one we encounter. The rest // are just the result of the first, and have no meaning. if (!Failed) @@ -528,6 +534,8 @@ private: /// @brief Potential simple keys. SmallVector SimpleKeys; + + std::error_code *EC; }; } // end namespace yaml @@ -722,13 +730,15 @@ std::string yaml::escape(StringRef Input) { return EscapedInput; } -Scanner::Scanner(StringRef Input, SourceMgr &sm, bool ShowColors) - : SM(sm), ShowColors(ShowColors) { +Scanner::Scanner(StringRef Input, SourceMgr &sm, bool ShowColors, + std::error_code *EC) + : SM(sm), ShowColors(ShowColors), EC(EC) { init(MemoryBufferRef(Input, "YAML")); } -Scanner::Scanner(MemoryBufferRef Buffer, SourceMgr &SM_, bool ShowColors) - : SM(SM_), ShowColors(ShowColors) { +Scanner::Scanner(MemoryBufferRef Buffer, SourceMgr &SM_, bool ShowColors, + std::error_code *EC) + : SM(SM_), ShowColors(ShowColors), EC(EC) { init(Buffer); } @@ -1726,11 +1736,13 @@ bool Scanner::fetchMoreTokens() { return false; } -Stream::Stream(StringRef Input, SourceMgr &SM, bool ShowColors) - : scanner(new Scanner(Input, SM, ShowColors)), CurrentDoc() {} +Stream::Stream(StringRef Input, SourceMgr &SM, bool ShowColors, + std::error_code *EC) + : scanner(new Scanner(Input, SM, ShowColors, EC)), CurrentDoc() {} -Stream::Stream(MemoryBufferRef InputBuffer, SourceMgr &SM, bool ShowColors) - : scanner(new Scanner(InputBuffer, SM, ShowColors)), CurrentDoc() {} +Stream::Stream(MemoryBufferRef InputBuffer, SourceMgr &SM, bool ShowColors, + std::error_code *EC) + : scanner(new Scanner(InputBuffer, SM, ShowColors, EC)), CurrentDoc() {} Stream::~Stream() {} diff --git a/lib/Support/YAMLTraits.cpp b/lib/Support/YAMLTraits.cpp index 75fac20a8ed..99d2070cb6e 100644 --- a/lib/Support/YAMLTraits.cpp +++ b/lib/Support/YAMLTraits.cpp @@ -44,13 +44,10 @@ void IO::setContext(void *Context) { // Input //===----------------------------------------------------------------------===// -Input::Input(StringRef InputContent, - void *Ctxt, - SourceMgr::DiagHandlerTy DiagHandler, - void *DiagHandlerCtxt) - : IO(Ctxt), - Strm(new Stream(InputContent, SrcMgr)), - CurrentNode(nullptr) { +Input::Input(StringRef InputContent, void *Ctxt, + SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt) + : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)), + CurrentNode(nullptr) { if (DiagHandler) SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt); DocIterator = Strm->begin(); diff --git a/unittests/Support/YAMLIOTest.cpp b/unittests/Support/YAMLIOTest.cpp index bf70e749b4f..c3e18d33235 100644 --- a/unittests/Support/YAMLIOTest.cpp +++ b/unittests/Support/YAMLIOTest.cpp @@ -2368,3 +2368,11 @@ TEST(YAMLIO, TestMapWithContext) { out); out.clear(); } + +TEST(YAMLIO, InvalidInput) { + // polluting 1 value in the sequence + Input yin("---\n- foo: 3\n bar: 5\n1\n- foo: 3\n bar: 5\n...\n"); + std::vector Data; + yin >> Data; + EXPECT_TRUE((bool)yin.error()); +}