From 253af1a3ad501cad2490f9b1ccff01addcd8b8ea Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 12 May 2016 20:02:33 +0200 Subject: [PATCH] Throw if glob parsing fails. This also fixes the warning about an unused boolean variable. --- lib/glob2regex/CMakeLists.txt | 1 + .../include/glob2regex/exceptions.hpp | 40 +++++++++++++++ .../include/glob2regex/glob2regex.hpp | 1 + lib/glob2regex/src/exceptions.cpp | 49 +++++++++++++++++++ lib/glob2regex/src/glob_ast.cpp | 5 ++ test/unit/test_glob2regex.cpp | 10 ++++ 6 files changed, 106 insertions(+) create mode 100644 lib/glob2regex/include/glob2regex/exceptions.hpp create mode 100644 lib/glob2regex/src/exceptions.cpp diff --git a/lib/glob2regex/CMakeLists.txt b/lib/glob2regex/CMakeLists.txt index 1b2fdac..bf5561d 100644 --- a/lib/glob2regex/CMakeLists.txt +++ b/lib/glob2regex/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(${PROJECT_NAME} src/glob2regex.cpp src/glob_ast.cpp src/render_ast.cpp + src/exceptions.cpp ) target_include_directories(${PROJECT_NAME} SYSTEM diff --git a/lib/glob2regex/include/glob2regex/exceptions.hpp b/lib/glob2regex/include/glob2regex/exceptions.hpp new file mode 100644 index 0000000..2243d69 --- /dev/null +++ b/lib/glob2regex/include/glob2regex/exceptions.hpp @@ -0,0 +1,40 @@ +/* Copyright 2015, 2016, Michele Santullo + * This file is part of "dindexer". + * + * "dindexer" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "dindexer" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "dindexer". If not, see . + */ + +#ifndef id998F0978CB064D728DDD7BE77F1FD9DC +#define id998F0978CB064D728DDD7BE77F1FD9DC + +#include +#include + +namespace g2r { + class ParsingError : public std::runtime_error { + public: + ParsingError ( void ) = delete; + ~ParsingError ( void ) noexcept = default; + ParsingError ( const std::string& parGlob, unsigned int parPosition ); + + const std::string& glob ( void ) const noexcept; + unsigned int position ( void ) const noexcept; + + private: + std::string m_glob; + unsigned int m_position; + }; +} //namespace g2r + +#endif diff --git a/lib/glob2regex/include/glob2regex/glob2regex.hpp b/lib/glob2regex/include/glob2regex/glob2regex.hpp index f179233..b247f85 100644 --- a/lib/glob2regex/include/glob2regex/glob2regex.hpp +++ b/lib/glob2regex/include/glob2regex/glob2regex.hpp @@ -18,6 +18,7 @@ #ifndef id98DC1C17239B4DD38C8697EC91BC3DA4 #define id98DC1C17239B4DD38C8697EC91BC3DA4 +#include "glob2regex/exceptions.hpp" #include namespace g2r { diff --git a/lib/glob2regex/src/exceptions.cpp b/lib/glob2regex/src/exceptions.cpp new file mode 100644 index 0000000..95a84db --- /dev/null +++ b/lib/glob2regex/src/exceptions.cpp @@ -0,0 +1,49 @@ +/* Copyright 2015, 2016, Michele Santullo + * This file is part of "dindexer". + * + * "dindexer" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "dindexer" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "dindexer". If not, see . + */ + +#include "glob2regex/exceptions.hpp" +#include +#include + +namespace g2r { + namespace { + std::string compose_err_msg (const std::string& parGlob, unsigned int parPosition) { + assert(parPosition <= parGlob.size()); + + std::ostringstream oss; + oss << "Error parsing glob \"" << parGlob << "\" at character " << (parPosition + 1); + + return oss.str(); + } + } //unnamed namespace + + ParsingError::ParsingError (const std::string& parGlob, unsigned int parPosition) : + std::runtime_error(compose_err_msg(parGlob, parPosition)), + m_glob(parGlob), + m_position(parPosition) + { + } + + const std::string& ParsingError::glob() const noexcept { + return m_glob; + } + + unsigned int ParsingError::position() const noexcept { + return m_position; + } +} //namespace g2r + diff --git a/lib/glob2regex/src/glob_ast.cpp b/lib/glob2regex/src/glob_ast.cpp index 913f411..55d7f02 100644 --- a/lib/glob2regex/src/glob_ast.cpp +++ b/lib/glob2regex/src/glob_ast.cpp @@ -16,6 +16,7 @@ */ #include "glob_ast.hpp" +#include "glob2regex/exceptions.hpp" #include //#include //#include @@ -30,6 +31,7 @@ #include #include #include +#include //#define AST_DEBUG_OUTPUT @@ -111,6 +113,9 @@ namespace g2r { std::cout << ", glob_ast.size() = " << glob_ast.size() << std::endl; #endif + if (not parse_ret or parGlob.end() != glob_beg) + throw ParsingError(parGlob, parGlob.end() - glob_beg); + return glob_ast; } } //namespace g2r diff --git a/test/unit/test_glob2regex.cpp b/test/unit/test_glob2regex.cpp index 5e617d1..952d913 100644 --- a/test/unit/test_glob2regex.cpp +++ b/test/unit/test_glob2regex.cpp @@ -61,4 +61,14 @@ TEST(glob2regex, convert) { const auto auto_regex = g2r::convert(glob); EXPECT_EQ(expected_regex, auto_regex); } + + { + const std::string glob = "nested_{example,*sample,test"; + EXPECT_THROW(g2r::convert(glob), g2r::ParsingError); + } + + { + const std::string glob = "]"; + EXPECT_THROW(g2r::convert(glob), g2r::ParsingError); + } }