Parser (function parseSrc) checks if there is equal number of [ and ] brackets but it's not enough to say that code is acceptable. What happens if there's "+][" code injected? Parser would say it's okay. And the code execution? Will there be a runtime error or infinite loop?
It can be solved by using a bit different check method: int a=0. When '[' is found increment a, but when ']' is found first check if a>0. If not throw exception, else decrement a.
After reaching the end of commands, check if a!=0. If not throw an exception. Just swap the second for-loop in ParseSrc with this one:
for(int i = 0; i < source.Length; i++)
switch (source[i])
{
case '[': bracketCount++; break;
case ']':
if(bracketCount<=0){ MessageBox.Show("Syntax error"); return null; }
bracketCount--; break;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Logged In: YES
user_id=1856779
Originator: YES
It can be solved by using a bit different check method: int a=0. When '[' is found increment a, but when ']' is found first check if a>0. If not throw exception, else decrement a.
After reaching the end of commands, check if a!=0. If not throw an exception. Just swap the second for-loop in ParseSrc with this one:
for(int i = 0; i < source.Length; i++)
switch (source[i])
{
case '[': bracketCount++; break;
case ']':
if(bracketCount<=0){ MessageBox.Show("Syntax error"); return null; }
bracketCount--; break;
}