From 644ae320a46a81c7e317fc5af34a68249330d92e Mon Sep 17 00:00:00 2001 From: hrkrshnn Date: Fri, 24 Apr 2020 15:40:04 +0530 Subject: [PATCH] Fixed infinite loops --- tools/solidityUpgrade/SourceTransform.h | 87 ++++++++++++++++++------- 1 file changed, 63 insertions(+), 24 deletions(-) diff --git a/tools/solidityUpgrade/SourceTransform.h b/tools/solidityUpgrade/SourceTransform.h index 08dfa3f35..b9250ae6a 100644 --- a/tools/solidityUpgrade/SourceTransform.h +++ b/tools/solidityUpgrade/SourceTransform.h @@ -50,7 +50,8 @@ public: * on a textual base. They utilize regular expression to search for * keywords or to determine formatting. */ -class SourceAnalysis { +class SourceAnalysis +{ public: static bool isMultilineKeyword( langutil::SourceLocation const& _location, @@ -103,6 +104,7 @@ public: for (auto inheritedContract: _contracts) overrideList += inheritedContract->name() + ","; + // Note: Can create incorrect replacements return "override(" + overrideList.substr(0, overrideList.size() - 1) + ")"; } }; @@ -123,11 +125,21 @@ public: std::string const& _expression ) { - return regex_replace( - _location.text(), - std::regex{"(\\b" + _keyword + "\\b)"}, - _expression + " " + _keyword - ); + auto _regex = std::regex{"(\\b" + _keyword + "\\b)"}; + if (regex_search(_location.text(), _regex)) + return regex_replace( + _location.text(), + _regex, + _expression + " " + _keyword + ); + else + solAssert( + false, + LocationHelper() << "Could not fix: " << _location.text() << " at " << _location << + "\nNeeds to be fixed manually." + ); + + return ""; } /// Searches for the keyword given and appends the expression. @@ -142,7 +154,17 @@ public: std::string toAppend = isMultiline ? ("\n " + _expression) : (" " + _expression); std::regex keyword{"(\\b" + _keyword + "\\b)"}; - return regex_replace(_location.text(), keyword, _keyword + toAppend); + if (regex_search(_location.text(), keyword)) + return regex_replace(_location.text(), keyword, _keyword + toAppend); + else + solAssert( + false, + LocationHelper() << "Could not fix: " << _location.text() << " at " << _location << + "\nNeeds to be fixed manually." + ); + + return ""; + } /// Searches for the first right parenthesis and appends the expression @@ -153,11 +175,21 @@ public: std::string const& _expression ) { - return regex_replace( - _location.text(), - std::regex{"(\\))"}, - ") " + _expression - ); + auto _regex = std::regex{"(\\))"}; + if (regex_search(_location.text(), _regex)) + return regex_replace( + _location.text(), + std::regex{"(\\))"}, + ") " + _expression + ); + else + solAssert( + false, + LocationHelper() << "Could not fix: " << _location.text() << " at " << _location << + "\nNeeds to be fixed manually." + ); + + return ""; } /// Searches for the `function` keyword and its identifier and replaces @@ -169,11 +201,21 @@ public: std::string const& _expression ) { - return regex_replace( - _location.text(), - std::regex{"(\\bfunction\\s*" + _name + "\\b)"}, - _expression - ); + auto _regex = std::regex{ "(\\bfunction\\s*" + _name + "\\b)"}; + if (regex_search(_location.text(), _regex)) + return regex_replace( + _location.text(), + _regex, + _expression + ); + else + solAssert( + false, + LocationHelper() << "Could not fix: " << _location.text() << " at " << _location << + "\nNeeds to be fixed manually." + ); + + return ""; } static std::string gasUpdate(langutil::SourceLocation const& _location) @@ -194,12 +236,10 @@ public: else solAssert( false, - LocationHelper() - << "Regex count not match: " << _location.text() << " at " << _location - << "\nNeeds to be fixed manually." + LocationHelper() << "Could not fix: " << _location.text() << " at " << _location << + "\nNeeds to be fixed manually." ); - return ""; } @@ -221,9 +261,8 @@ public: else solAssert( false, - LocationHelper() - << "Regex count not match: " << _location.text() << " at " << _location - << "\nNeeds to be fixed manually" + LocationHelper() << "Could not fix: " << _location.text() << " at " << _location << + "\nNeeds to be fixed manually." ); return "";