노현종

GetCriticalBlock Syntax 기반 구현

...@@ -382,11 +382,18 @@ namespace VulnCrawler ...@@ -382,11 +382,18 @@ namespace VulnCrawler
382 { 382 {
383 return false; 383 return false;
384 } 384 }
385 +
386 + /* 대문자로 구성된 변수면 넘어감 */
387 + if (m.Value.All(c => char.IsUpper(c) || !char.IsLetter(c)))
388 + {
389 + return false;
390 + }
385 /* 제일 앞자리가 숫자로 시작하면 넘어감 */ 391 /* 제일 앞자리가 숫자로 시작하면 넘어감 */
386 if (Regex.IsMatch(m.Value, invalidPattern)) 392 if (Regex.IsMatch(m.Value, invalidPattern))
387 { 393 {
388 return false; 394 return false;
389 } 395 }
396 +
390 /* 전 단계에서 구한 메서드 목록에 있으면 넘어감 */ 397 /* 전 단계에서 구한 메서드 목록에 있으면 넘어감 */
391 if (methodSets.Contains(m.Value)) 398 if (methodSets.Contains(m.Value))
392 { 399 {
...@@ -403,6 +410,8 @@ namespace VulnCrawler ...@@ -403,6 +410,8 @@ namespace VulnCrawler
403 { 410 {
404 return false; 411 return false;
405 } 412 }
413 +
414 +
406 return true; 415 return true;
407 }) 416 })
408 .Distinct(new MatchComparer()); 417 .Distinct(new MatchComparer());
......
...@@ -232,7 +232,6 @@ namespace VulnCrawler ...@@ -232,7 +232,6 @@ namespace VulnCrawler
232 // srcCode = Regex.Replace(srcCode, @"if.+\n\{", @"if.+\{", RegexOptions.Multiline); 232 // srcCode = Regex.Replace(srcCode, @"if.+\n\{", @"if.+\{", RegexOptions.Multiline);
233 233
234 var split = srcCode.Split('\n'); 234 var split = srcCode.Split('\n');
235 - int bracketCount = 0;
236 var blockList = new List<Block>(); 235 var blockList = new List<Block>();
237 StringBuilder builder = new StringBuilder(); 236 StringBuilder builder = new StringBuilder();
238 var crList = criticalList as HashSet<string>; 237 var crList = criticalList as HashSet<string>;
...@@ -240,68 +239,141 @@ namespace VulnCrawler ...@@ -240,68 +239,141 @@ namespace VulnCrawler
240 { 239 {
241 return null; 240 return null;
242 } 241 }
243 - bool mainLine = true; /* 현재 라인이 메인 코드 라인인지 */
244 -
245 - int blockNum = 1; /* 블록 번호 */
246 -
247 242
248 - bool group = false;
249 - Queue<string> groupQ = new Queue<string>();
250 var mainQ = new Queue<string>(); 243 var mainQ = new Queue<string>();
251 - 244 + var groupQ = new Queue<string>();
245 + bool mainLine = true;
246 + int crNum = 1;
247 + int bracketCount = 1;
248 + bool prevStartBlock = false;
252 foreach (var line in split) 249 foreach (var line in split)
253 { 250 {
254 - bool criticalBlock = false; /* 현재 라인이 크리티컬 블록 라인인지 */ 251 + bool criticalBlock = false;
252 + string trimLine = line.Trim();
253 + if (mainLine)
254 + {
255 + if (trimLine.EndsWith("&&") || trimLine.EndsWith("||"))
256 + {
257 + mainQ.Enqueue(line);
258 + continue;
259 + }
255 260
256 - string trim = line.Trim(); 261 + if (trimLine.StartsWith("&&") || trimLine.StartsWith("||"))
262 + {
263 + groupQ.Enqueue(line);
264 + continue;
265 + }
257 266
258 - if (Regex.IsMatch(trim, @"^(if|for|while)")) 267 +
259 - { 268 +
260 - group = true; 269 + bracketCount = 1;
261 - mainLine = false; 270 + StringBuilder groupBuilder = new StringBuilder();
262 - groupQ.Enqueue(line); 271 + while(groupQ.Count > 0)
263 - if (trim.EndsWith("{"))
264 { 272 {
265 - group = true; 273 + string s = groupQ.Dequeue();
274 + if (!criticalBlock)
275 + {
276 + foreach (var item in ExtractCriticalVariant(s))
277 + {
278 + if (crList.Contains(item))
279 + {
280 + criticalBlock = true;
281 + break;
282 + }
283 + }
284 + }
285 + groupBuilder.AppendLine(s);
266 } 286 }
267 - else if (trim.EndsWith("}")) 287 +
288 + if (groupBuilder.Length > 0)
268 { 289 {
269 - group = false; 290 + blockList.Add(new Block { Code = groupBuilder.ToString(), HasCritical = criticalBlock, Num = crNum++});
270 } 291 }
271 - else if(trim.EndsWith(";")) 292 + if (Regex.IsMatch(trimLine, @"(if|for|while|switch|do)\s*"))
272 { 293 {
273 - group = false; 294 + if (!trimLine.EndsWith("{"))
295 + {
296 + mainLine = false;
297 + prevStartBlock = true;
298 +
299 + }
300 + else if (trimLine.EndsWith(";"))
301 + {
302 + mainLine = true;
303 + }
304 + else
305 + {
306 + mainLine = false;
307 + bracketCount++;
308 + }
309 +
310 + groupQ.Enqueue(line);
311 +
312 +
313 + continue;
274 } 314 }
275 - continue; 315 + mainQ.Enqueue(line);
276 } 316 }
277 - 317 + else
278 - if (group)
279 { 318 {
280 - groupQ.Enqueue(line); 319 +
281 - if (trim.EndsWith("}")) 320 +
321 + /* 중괄호 수 세기 */
322 + int openBracketCount = trimLine.Count(c => c == '{');
323 + int closeBracketCount = trimLine.Count(c => c == '}');
324 + int subtract = openBracketCount - closeBracketCount;
325 + bracketCount += subtract;
326 +
327 +
328 + if (trimLine.EndsWith("&&") || trimLine.EndsWith("||"))
282 { 329 {
283 - group = false; 330 + groupQ.Enqueue(line);
331 + continue;
284 } 332 }
285 - else if (trim.EndsWith(";")) 333 +
334 + //if (trimLine.StartsWith("&&") || trimLine.StartsWith("||"))
335 + //{
336 + // mainQ.Enqueue(line);
337 + // continue;
338 +
339 + //}
340 + groupQ.Enqueue(line);
341 + if (prevStartBlock)
286 { 342 {
287 - group = false; 343 + prevStartBlock = false;
344 + if (Regex.IsMatch(trimLine, @"(if|for|while|switch|do)\s*\("))
345 + {
346 + prevStartBlock = true;
347 + continue;
348 +
349 + }
350 + else if(trimLine.EndsWith(";"))
351 + {
352 + bracketCount--;
353 + }
288 } 354 }
289 - continue;
290 - }
291 355
292 - mainQ.Enqueue(line); 356 + if (bracketCount <= 1)
357 + {
358 + if (trimLine.Contains("else"))
359 + {
360 + bracketCount++;
361 + prevStartBlock = true;
362 + continue;
363 + }
293 364
294 - StringBuilder mainBuilder = new StringBuilder(); 365 + mainLine = true;
295 - if (!mainLine) 366 + }
296 - { 367 +
297 - while(mainQ.Count > 0) 368 + StringBuilder mainBuilder = new StringBuilder();
369 + while (mainQ.Count > 0)
298 { 370 {
299 string s = mainQ.Dequeue(); 371 string s = mainQ.Dequeue();
300 if (!criticalBlock) 372 if (!criticalBlock)
301 { 373 {
302 - foreach (var var in ExtractCriticalVariant(s)) 374 + foreach (var item in ExtractCriticalVariant(s))
303 { 375 {
304 - if (crList.Contains(var)) 376 + if (crList.Contains(item))
305 { 377 {
306 criticalBlock = true; 378 criticalBlock = true;
307 break; 379 break;
...@@ -310,47 +382,70 @@ namespace VulnCrawler ...@@ -310,47 +382,70 @@ namespace VulnCrawler
310 } 382 }
311 mainBuilder.AppendLine(s); 383 mainBuilder.AppendLine(s);
312 } 384 }
385 +
313 if (mainBuilder.Length > 0) 386 if (mainBuilder.Length > 0)
314 { 387 {
315 - blockList.Add(new Block { Code = mainBuilder.ToString(), HasCritical = criticalBlock, Num = blockNum++ }); 388 + blockList.Add(new Block { Code = mainBuilder.ToString(), HasCritical = criticalBlock, Num = crNum++ });
316 - //continue; 389 + }
390 +
391 +
392 + }
393 +
394 +
395 + }
396 +
397 + bool cb = false;
398 + if (mainQ.Count > 0)
399 + {
400 + StringBuilder mainBuilder = new StringBuilder();
401 + while (mainQ.Count > 0)
402 + {
403 + string s = mainQ.Dequeue();
404 + if (!cb)
405 + {
406 + foreach (var item in ExtractCriticalVariant(s))
407 + {
408 + if (crList.Contains(item))
409 + {
410 + cb = true;
411 + break;
412 + }
413 + }
317 } 414 }
415 + mainBuilder.AppendLine(s);
318 } 416 }
319 417
418 + if (mainBuilder.Length > 0)
419 + {
420 + blockList.Add(new Block { Code = mainBuilder.ToString(), HasCritical = cb, Num = crNum++ });
421 + }
422 + }
423 + else
424 + {
320 StringBuilder groupBuilder = new StringBuilder(); 425 StringBuilder groupBuilder = new StringBuilder();
321 while (groupQ.Count > 0) 426 while (groupQ.Count > 0)
322 { 427 {
323 - var s = groupQ.Dequeue(); 428 + string s = groupQ.Dequeue();
324 - if (!criticalBlock) 429 + if (!cb)
325 { 430 {
326 - foreach (var var in ExtractCriticalVariant(s)) 431 + foreach (var item in ExtractCriticalVariant(s))
327 { 432 {
328 - if (crList.Contains(var)) 433 + if (crList.Contains(item))
329 { 434 {
330 - criticalBlock = true; 435 + cb = true;
331 break; 436 break;
332 } 437 }
333 } 438 }
334 } 439 }
335 groupBuilder.AppendLine(s); 440 groupBuilder.AppendLine(s);
336 } 441 }
442 +
337 if (groupBuilder.Length > 0) 443 if (groupBuilder.Length > 0)
338 { 444 {
339 - blockList.Add(new Block { Code = groupBuilder.ToString(), HasCritical = criticalBlock, Num = blockNum++ }); 445 + blockList.Add(new Block { Code = groupBuilder.ToString(), HasCritical = cb, Num = crNum++ });
340 - continue;
341 } 446 }
342 -
343 -
344 -
345 -
346 - mainLine = true;
347 -
348 -
349 -
350 -
351 } 447 }
352 448
353 - Console.WriteLine("끝");
354 return blockList; 449 return blockList;
355 } 450 }
356 } 451 }
......