Skip to content

Commit

Permalink
Fixed critical issue + operator ? evaluation
Browse files Browse the repository at this point in the history
- Fixed critical issue: infinite loop while interpreting an expression in a large folder #32
- operator ? misevaluation
- the implementation of the fix with a blacklist could be improved
  • Loading branch information
ProSurfer73 committed Sep 15, 2022
1 parent 9126f82 commit 2d836ea
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 30 deletions.
12 changes: 6 additions & 6 deletions Project/MacroParser.depend
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@
<iostream>
"config.hpp"

1663182647 source:c:\git\macro-parser\project\filesystem.cpp
1663244741 source:c:\git\macro-parser\project\filesystem.cpp
"filesystem.hpp"
"options.hpp"
"stringeval.hpp"

1663179980 c:\git\macro-parser\project\filesystem.hpp
1663258717 c:\git\macro-parser\project\filesystem.hpp
<string>
<vector>
<fstream>
Expand All @@ -226,7 +226,7 @@
<mutex>
<windows.h>

1663100780 c:\git\macro-parser\project\stringeval.hpp
1663242424 c:\git\macro-parser\project\stringeval.hpp
<vector>
<string>
<sstream>
Expand Down Expand Up @@ -275,13 +275,13 @@
"options.hpp"
"container.hpp"

1663184835 source:c:\git\macro-parser\project\stringeval.cpp
1663267845 source:c:\git\macro-parser\project\stringeval.cpp
"stringeval.hpp"
<iostream>
"container.hpp"
"options.hpp"

1663185344 source:c:\git\macro-parser\project\command.cpp
1663260086 source:c:\git\macro-parser\project\command.cpp
"command.hpp"
"container.hpp"
"stringeval.hpp"
Expand All @@ -293,7 +293,7 @@
"filesystem.hpp"
"stringeval.hpp"

1663148232 source:c:\git\macro-parser\project\container.cpp
1663247787 source:c:\git\macro-parser\project\container.cpp
"container.hpp"

1661953014 source:c:\git\macro-parser\project\booleval.cpp
Expand Down
Binary file modified Project/bin/Release/MacroParser.exe
Binary file not shown.
16 changes: 11 additions & 5 deletions Project/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ bool CommandManager::runCommand(string input)
else if(parameters.front() == "look")
{

clearBlacklist();

/*if(parameters.size()>2 && !macrospaces.doesMacrospaceExists(parameters[2]))
cout << "The macrospace '" << parameters[1] << "' does not exist." << endl;
else*/
Expand Down Expand Up @@ -359,7 +361,8 @@ bool CommandManager::runCommand(string input)
{
std::vector<std::string> results;
string putput=p.second;
auto status = calculateExpression(putput, macrospaces.getMacroSpace(commandMacrospaces.front()), configuration, true, true, &results);
auto status = calculateExpression(putput, macrospaces.getMacroSpace(commandMacrospaces.front()), configuration, true, true, &results);//&results);
found=true;

if(results.size()>1)
{
Expand All @@ -368,7 +371,7 @@ bool CommandManager::runCommand(string input)
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());

std::cout << "Possible results: ";
std::cout << results.size() << " possible results: ";
for(unsigned i=0; i<results.size(); ++i){
if(results[i]!="multiple"){
std::cout << results[i];
Expand Down Expand Up @@ -410,7 +413,7 @@ bool CommandManager::runCommand(string input)
if(status == CalculationStatus::EVAL_ERROR ||status == CalculationStatus::EVAL_OKAY)
cout << endl;

found=true;

break;
}

Expand Down Expand Up @@ -649,6 +652,8 @@ bool CommandManager::runCommand(string input)
}
else if(commandStr == "evaluate")
{
clearBlacklist();

string expr = input.substr(9);

// Extract a macrospace from the command
Expand All @@ -669,14 +674,14 @@ bool CommandManager::runCommand(string input)
std::vector<std::string> results;
auto status = calculateExpression(expr, *mc, configuration, true, true, &results);

if(results.size()>1)
if(!results.empty())
{
// Sort and remove duplicates
auto& v = results;
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());

std::cout << "Possible results: ";
std::cout << results.size() << " possible results: ";
for(unsigned i=0; i<results.size(); ++i){
if(results[i] != "multiple"){
std::cout << results[i];
Expand Down Expand Up @@ -888,6 +893,7 @@ bool CommandManager::runCommand(string input)
printBasicHelp();
else if(commandStr == "cls"){
system("cls");

}
else if(commandStr == "loadscript")
{
Expand Down
2 changes: 1 addition & 1 deletion Project/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ void MacroContainer::printDiff(std::vector<MacroContainer*>& mcs, const Options&
{
std::string str = itf->second;


clearBlacklist();

calculateExprWithStrOutput(str, *mc, configuration, false);

Expand Down
4 changes: 3 additions & 1 deletion Project/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ bool FileSystem::directoryExists(const char* szPath)
#endif


bool FileSystem::importFile(const string& pathToFile, MacroDatabase& macroContainer, const Options& config)
bool FileSystem::importFile(const string& pathToFile, MacroDatabase& macroContainer, const Options& config)
{
ifstream file(pathToFile);

if(!file.is_open())
return false;

clearBlacklist();

#ifdef DEBUG_LOG_FILE_IMPORT
cout << "Opened " << pathToFile << endl;
#endif
Expand Down
2 changes: 1 addition & 1 deletion Project/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@



//#define ENABLE_MUTEX_LOADINGBAR
#define ENABLE_MUTEX_LOADINGBAR

using namespace std;

Expand Down
129 changes: 113 additions & 16 deletions Project/stringeval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
******************************************************************************
*/



#include "stringeval.hpp"

#include <iostream>
#include "container.hpp"
#include "options.hpp"

std::vector<std::string> blacklist;

void clearBlacklist()
{
blacklist.clear();
}

void clearSpaces(string& str)
{
str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
Expand Down Expand Up @@ -336,9 +345,28 @@ static bool treatInterrogationOperator(std::string& expr, const MacroContainer&
// As long as we find an interrogation operator
while((searchedInterrogation=expr.find('?')) != std::string::npos)
{
std::string theleft = expr.substr(0, searchedInterrogation);
// not to much left
int abcd=0;
std::size_t thepos=searchedInterrogation;
for(; thepos>0; --thepos){
if(expr[thepos] == '(')
{
abcd--;
if(abcd <= 0){
break;
}
}
else if(expr[thepos] == ')')
abcd--;
}


std::string theleft = expr.substr(thepos, searchedInterrogation);
std::string theright = expr.substr(searchedInterrogation+1);

/*std::cout << "theleft: " << theleft << std::endl;
std::cout << "theright: " << theright << std::endl;*/

// Let's evaluate the left part
auto status = calculateExpression(theleft, mc, config, false, true);

Expand All @@ -349,7 +377,30 @@ static bool treatInterrogationOperator(std::string& expr, const MacroContainer&
if( (kk=theright.find(':')) != std::string::npos)
{
if(theleft == "false")
expr = theright.substr(kk+1);
{
// not too much right
int abcde=0;
std::size_t thepos2=kk;
for(; thepos2>0; --thepos2){
if(expr[thepos2] == ')')
{
abcde--;
if(abcde <= 0){
break;
}
}
else if(expr[thepos] == '(')
abcde--;
}

// todo: fix not too much right
expr = theright.substr(kk+1, thepos2-(kk+1));

//std::cout << "newexpr:" << expr << std::endl;


}

else if(theleft == "true")
expr = theright.substr(0, kk);

Expand All @@ -372,6 +423,24 @@ static bool treatInterrogationOperator(std::string& expr, const MacroContainer&
return didSomething;
}

static bool emplaceOnce(std::vector< std::string >& v, const std::string& macroName)
{
if(v.empty()){
v.emplace_back(macroName);
return true;
}

if(std::find(v.begin(), v.end(), macroName)==v.end())
{
v.emplace_back(macroName);
return true;
}

return false;
}



/** \brief
*
* \param expr: macro given in input
Expand All @@ -382,6 +451,8 @@ static bool treatInterrogationOperator(std::string& expr, const MacroContainer&
enum CalculationStatus calculateExpression(string& expr, const MacroContainer& macroContainer, const Options& config,
bool printWarnings, bool enableBoolean, std::vector<std::string>* outputs, std::vector< std::pair<std::string, std::string> > redef)
{
//if(printWarnings || outputs!=nullptr) std::cout << expr << std::endl;

CalculationStatus status = CalculationStatus::EVAL_OKAY;

const auto& dictionary = macroContainer.getDefines();
Expand Down Expand Up @@ -508,21 +579,38 @@ bool printWarnings, bool enableBoolean, std::vector<std::string>* outputs, std::

if(outputs)
{
bool oneThing=false;

for(const auto& pp: dictionary)
{
if(pp.first == p.first)
if(pp.first == p.first && p.first != expr &&
std::find(outputs->begin(), outputs->end(), expr) == outputs->end())
{
std::string anotherExpr = expr;
simpleReplace(anotherExpr, p.first, pp.second);
redef.emplace_back(p.first, pp.second);
auto status = calculateExpression(anotherExpr, macroContainer, config, false, true, outputs, redef);
if(status != CalculationStatus::EVAL_ERROR)
outputs->emplace_back(anotherExpr);
while(simpleReplace(anotherExpr, p.first, pp.second));
//redef.emplace_back(p.first, pp.second);

if(std::find(blacklist.begin(), blacklist.end(), pp.second) == blacklist.end())
{
oneThing=true;
blacklist.emplace_back(pp.second);

auto status = calculateExpression(anotherExpr, macroContainer, config, false, true, outputs, redef);
if(status == CalculationStatus::EVAL_OKAY)
emplaceOnce(*outputs, anotherExpr);
}


}
}

expr = "multiple";
return CalculationStatus::EVAL_WARNING;
if(oneThing)
{
expr = "multiple";
return CalculationStatus::EVAL_WARNING;
}


}

status = CalculationStatus::EVAL_WARNING;
Expand Down Expand Up @@ -772,7 +860,7 @@ bool printWarnings, bool enableBoolean, std::vector<std::string>* outputs, std::
endStr = &expr[posClosePar+1];

// If it is a number, let's just remove the parentheses for now
/*bool isOnlyNumbers=true;
bool isOnlyNumbers=true;
for(char c: subExpr){
if(!(c == '.' || isdigit(c)))
isOnlyNumbers=false;
Expand All @@ -783,7 +871,7 @@ bool printWarnings, bool enableBoolean, std::vector<std::string>* outputs, std::
expr = begStr+subExpr+endStr;
repeat=true;
goto restartArithmeticEval;
}*/
}

// Let's to reevaluate what is in the parenthesis
std::string subExpr2 = subExpr;
Expand Down Expand Up @@ -830,7 +918,7 @@ bool printWarnings, bool enableBoolean, std::vector<std::string>* outputs, std::
{
expr = begStr+subExpr+endStr;
repeat=true;
goto restartArithmeticEval;
//goto restartArithmeticEval;
}

if(repeat)
Expand Down Expand Up @@ -903,12 +991,14 @@ void calculateExprWithStrOutput(string& expr, const MacroContainer& macroContain
auto status = calculateExpression(expr, macroContainer, options, false, true, &output);
auto& results = output;

clearBlacklist();

// Sort and remove duplicates
/*auto& v = output;
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());*/

if(!results.empty())
if(!results.empty() || results.size()>=1)
{
expr.clear();

Expand All @@ -927,7 +1017,8 @@ void calculateExprWithStrOutput(string& expr, const MacroContainer& macroContain
}
}

expr += " ?";
if(results.size()>1)
expr += " ?";
}
else
{
Expand All @@ -936,7 +1027,13 @@ void calculateExprWithStrOutput(string& expr, const MacroContainer& macroContain
if(status == CalculationStatus::EVAL_ERROR)
expr = "unknown";
else if(status == CalculationStatus::EVAL_WARNING)
expr += "??";
{

{
expr += "??";
}
}

}

}
1 change: 1 addition & 0 deletions Project/stringeval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "stringeval.hpp"
#include "hexa.hpp"

void clearBlacklist();

using namespace std;

Expand Down

0 comments on commit 2d836ea

Please sign in to comment.