노현종

GetCriticalBlock Syntax 기반 구현

......@@ -382,11 +382,18 @@ namespace VulnCrawler
{
return false;
}
/* 대문자로 구성된 변수면 넘어감 */
if (m.Value.All(c => char.IsUpper(c) || !char.IsLetter(c)))
{
return false;
}
/* 제일 앞자리가 숫자로 시작하면 넘어감 */
if (Regex.IsMatch(m.Value, invalidPattern))
{
return false;
}
/* 전 단계에서 구한 메서드 목록에 있으면 넘어감 */
if (methodSets.Contains(m.Value))
{
......@@ -403,6 +410,8 @@ namespace VulnCrawler
{
return false;
}
return true;
})
.Distinct(new MatchComparer());
......
......@@ -232,7 +232,6 @@ namespace VulnCrawler
// srcCode = Regex.Replace(srcCode, @"if.+\n\{", @"if.+\{", RegexOptions.Multiline);
var split = srcCode.Split('\n');
int bracketCount = 0;
var blockList = new List<Block>();
StringBuilder builder = new StringBuilder();
var crList = criticalList as HashSet<string>;
......@@ -240,68 +239,141 @@ namespace VulnCrawler
{
return null;
}
bool mainLine = true; /* 현재 라인이 메인 코드 라인인지 */
int blockNum = 1; /* 블록 번호 */
bool group = false;
Queue<string> groupQ = new Queue<string>();
var mainQ = new Queue<string>();
var groupQ = new Queue<string>();
bool mainLine = true;
int crNum = 1;
int bracketCount = 1;
bool prevStartBlock = false;
foreach (var line in split)
{
bool criticalBlock = false; /* 현재 라인이 크리티컬 블록 라인인지 */
bool criticalBlock = false;
string trimLine = line.Trim();
if (mainLine)
{
if (trimLine.EndsWith("&&") || trimLine.EndsWith("||"))
{
mainQ.Enqueue(line);
continue;
}
string trim = line.Trim();
if (trimLine.StartsWith("&&") || trimLine.StartsWith("||"))
{
groupQ.Enqueue(line);
continue;
}
if (Regex.IsMatch(trim, @"^(if|for|while)"))
{
group = true;
mainLine = false;
groupQ.Enqueue(line);
if (trim.EndsWith("{"))
bracketCount = 1;
StringBuilder groupBuilder = new StringBuilder();
while(groupQ.Count > 0)
{
group = true;
string s = groupQ.Dequeue();
if (!criticalBlock)
{
foreach (var item in ExtractCriticalVariant(s))
{
if (crList.Contains(item))
{
criticalBlock = true;
break;
}
}
}
groupBuilder.AppendLine(s);
}
else if (trim.EndsWith("}"))
if (groupBuilder.Length > 0)
{
group = false;
blockList.Add(new Block { Code = groupBuilder.ToString(), HasCritical = criticalBlock, Num = crNum++});
}
else if(trim.EndsWith(";"))
if (Regex.IsMatch(trimLine, @"(if|for|while|switch|do)\s*"))
{
group = false;
if (!trimLine.EndsWith("{"))
{
mainLine = false;
prevStartBlock = true;
}
else if (trimLine.EndsWith(";"))
{
mainLine = true;
}
else
{
mainLine = false;
bracketCount++;
}
groupQ.Enqueue(line);
continue;
}
continue;
mainQ.Enqueue(line);
}
if (group)
else
{
groupQ.Enqueue(line);
if (trim.EndsWith("}"))
/* 중괄호 수 세기 */
int openBracketCount = trimLine.Count(c => c == '{');
int closeBracketCount = trimLine.Count(c => c == '}');
int subtract = openBracketCount - closeBracketCount;
bracketCount += subtract;
if (trimLine.EndsWith("&&") || trimLine.EndsWith("||"))
{
group = false;
groupQ.Enqueue(line);
continue;
}
else if (trim.EndsWith(";"))
//if (trimLine.StartsWith("&&") || trimLine.StartsWith("||"))
//{
// mainQ.Enqueue(line);
// continue;
//}
groupQ.Enqueue(line);
if (prevStartBlock)
{
group = false;
prevStartBlock = false;
if (Regex.IsMatch(trimLine, @"(if|for|while|switch|do)\s*\("))
{
prevStartBlock = true;
continue;
}
else if(trimLine.EndsWith(";"))
{
bracketCount--;
}
}
continue;
}
mainQ.Enqueue(line);
if (bracketCount <= 1)
{
if (trimLine.Contains("else"))
{
bracketCount++;
prevStartBlock = true;
continue;
}
StringBuilder mainBuilder = new StringBuilder();
if (!mainLine)
{
while(mainQ.Count > 0)
mainLine = true;
}
StringBuilder mainBuilder = new StringBuilder();
while (mainQ.Count > 0)
{
string s = mainQ.Dequeue();
if (!criticalBlock)
{
foreach (var var in ExtractCriticalVariant(s))
foreach (var item in ExtractCriticalVariant(s))
{
if (crList.Contains(var))
if (crList.Contains(item))
{
criticalBlock = true;
break;
......@@ -310,47 +382,70 @@ namespace VulnCrawler
}
mainBuilder.AppendLine(s);
}
if (mainBuilder.Length > 0)
{
blockList.Add(new Block { Code = mainBuilder.ToString(), HasCritical = criticalBlock, Num = blockNum++ });
//continue;
blockList.Add(new Block { Code = mainBuilder.ToString(), HasCritical = criticalBlock, Num = crNum++ });
}
}
}
bool cb = false;
if (mainQ.Count > 0)
{
StringBuilder mainBuilder = new StringBuilder();
while (mainQ.Count > 0)
{
string s = mainQ.Dequeue();
if (!cb)
{
foreach (var item in ExtractCriticalVariant(s))
{
if (crList.Contains(item))
{
cb = true;
break;
}
}
}
mainBuilder.AppendLine(s);
}
if (mainBuilder.Length > 0)
{
blockList.Add(new Block { Code = mainBuilder.ToString(), HasCritical = cb, Num = crNum++ });
}
}
else
{
StringBuilder groupBuilder = new StringBuilder();
while (groupQ.Count > 0)
{
var s = groupQ.Dequeue();
if (!criticalBlock)
string s = groupQ.Dequeue();
if (!cb)
{
foreach (var var in ExtractCriticalVariant(s))
foreach (var item in ExtractCriticalVariant(s))
{
if (crList.Contains(var))
if (crList.Contains(item))
{
criticalBlock = true;
cb = true;
break;
}
}
}
groupBuilder.AppendLine(s);
}
if (groupBuilder.Length > 0)
{
blockList.Add(new Block { Code = groupBuilder.ToString(), HasCritical = criticalBlock, Num = blockNum++ });
continue;
blockList.Add(new Block { Code = groupBuilder.ToString(), HasCritical = cb, Num = crNum++ });
}
mainLine = true;
}
Console.WriteLine("끝");
return blockList;
}
}
......