노현종

Merge branch 'master' into master3

...@@ -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,121 +239,211 @@ namespace VulnCrawler ...@@ -240,121 +239,211 @@ namespace VulnCrawler
240 { 239 {
241 return null; 240 return null;
242 } 241 }
243 - bool hasIf = false; 242 +
244 - bool mainLine = true; /* 현재 라인이 메인 코드 라인인지 */ 243 + var mainQ = new Queue<string>();
245 - bool criticalBlock = false; /* 현재 라인이 크리티컬 블록 라인인지 */ 244 + var groupQ = new Queue<string>();
246 - int blockNum = 1; /* 블록 번호 */ 245 + bool mainLine = true;
246 + int crNum = 1;
247 + int bracketCount = 1;
248 + bool prevStartBlock = false;
247 foreach (var line in split) 249 foreach (var line in split)
248 { 250 {
249 - bool hasRight = 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 + }
250 260
251 - string trim = line.Trim(); 261 + if (trimLine.StartsWith("&&") || trimLine.StartsWith("||"))
262 + {
263 + groupQ.Enqueue(line);
264 + continue;
265 + }
252 266
253 - /* 중괄호 수 세기 */
254 - int openBracketCount = trim.Count(c => c == '{');
255 - int closeBracketCount = trim.Count(c => c == '}');
256 - //if (!hasIf)
257 - //{
258 - // if (Regex.IsMatch(trim, @"^if.+\)$"))
259 - // {
260 - // // Console.WriteLine("if 들어감");
261 - // hasIf = true;
262 - // }
263 - //}
264 - //else
265 - //{
266 - // if (!Regex.IsMatch(trim, @"^\{"))
267 - // {
268 - // openBracketCount++;
269 - // }
270 - // hasIf = false;
271 - //}
272 267
273 268
269 + bracketCount = 1;
270 + StringBuilder groupBuilder = new StringBuilder();
271 + while(groupQ.Count > 0)
272 + {
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);
286 + }
274 287
288 + if (groupBuilder.Length > 0)
289 + {
290 + blockList.Add(new Block { Code = groupBuilder.ToString(), HasCritical = criticalBlock, Num = crNum++});
291 + }
292 + if (Regex.IsMatch(trimLine, @"(if|for|while|switch|do)\s*"))
293 + {
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;
314 + }
315 + mainQ.Enqueue(line);
316 + }
317 + else
318 + {
319 +
320 +
321 + /* 중괄호 수 세기 */
322 + int openBracketCount = trimLine.Count(c => c == '{');
323 + int closeBracketCount = trimLine.Count(c => c == '}');
275 int subtract = openBracketCount - closeBracketCount; 324 int subtract = openBracketCount - closeBracketCount;
276 bracketCount += subtract; 325 bracketCount += subtract;
277 326
278 327
279 - if (trim.Equals("}")) 328 + if (trimLine.EndsWith("&&") || trimLine.EndsWith("||"))
280 { 329 {
281 - builder.AppendLine(line); 330 + groupQ.Enqueue(line);
282 - hasRight = true; 331 + continue;
283 } 332 }
284 - /* 중괄호 연산 결과 1이라는 것은 메인 라인 */ 333 +
285 - if (bracketCount == 1) 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 - /* 343 + prevStartBlock = false;
288 - * 깊이가 1인데 mainLine이 344 + if (Regex.IsMatch(trimLine, @"(if|for|while|switch|do)\s*\("))
289 - * false 이면 넘어왔다는 것이니 현재까지 코드
290 - * blockList에 추가
291 - */
292 - if (!mainLine)
293 { 345 {
294 - string s = builder.ToString(); 346 + prevStartBlock = true;
295 - if (!string.IsNullOrWhiteSpace(s)) 347 + continue;
348 +
349 + }
350 + else if(trimLine.EndsWith(";"))
296 { 351 {
297 - blockList.Add(new Block() { HasCritical = criticalBlock, Code = s, Num = blockNum }); 352 + bracketCount--;
298 - blockNum++;
299 - criticalBlock = false;
300 - builder.Clear();
301 } 353 }
302 } 354 }
355 +
356 + if (bracketCount <= 1)
357 + {
358 + if (trimLine.Contains("else"))
359 + {
360 + bracketCount++;
361 + prevStartBlock = true;
362 + continue;
363 + }
364 +
303 mainLine = true; 365 mainLine = true;
304 } 366 }
305 - /* 2 이상이라는 건 메인 라인 X */ 367 +
306 - else if(bracketCount >= 2) 368 + StringBuilder mainBuilder = new StringBuilder();
369 + while (mainQ.Count > 0)
307 { 370 {
308 - /* 371 + string s = mainQ.Dequeue();
309 - * 깊이가 2 이상인데 mainLine이 372 + if (!criticalBlock)
310 - * true면 넘어왔다는 것이니 현재까지 코드
311 - * blockList에 추가
312 - */
313 - if (mainLine)
314 { 373 {
315 - string s = builder.ToString(); 374 + foreach (var item in ExtractCriticalVariant(s))
316 - if (!string.IsNullOrWhiteSpace(s))
317 { 375 {
318 - blockList.Add(new Block() { HasCritical = criticalBlock, Code = s, Num = blockNum }); 376 + if (crList.Contains(item))
319 - blockNum++; 377 + {
320 - criticalBlock = false; 378 + criticalBlock = true;
321 - builder.Clear(); 379 + break;
322 } 380 }
323 } 381 }
324 - mainLine = false;
325 } 382 }
326 - /* 이도 저도 아니면 그냥 넘어감 */ 383 + mainBuilder.AppendLine(s);
327 - else 384 + }
385 +
386 + if (mainBuilder.Length > 0)
328 { 387 {
329 - continue; 388 + blockList.Add(new Block { Code = mainBuilder.ToString(), HasCritical = criticalBlock, Num = crNum++ });
389 + }
390 +
391 +
330 } 392 }
331 - /* 현재 코드 라인에서 변수 추출시켜서 크리티컬 리스트와 대조 */ 393 +
332 - foreach (var var in ExtractCriticalVariant(line)) 394 +
395 + }
396 +
397 + bool cb = false;
398 + if (mainQ.Count > 0)
333 { 399 {
334 - /* 크리티컬 리스트에 추출한 변수가 들어있다면 추가 */ 400 + StringBuilder mainBuilder = new StringBuilder();
335 - if (criticalList.Contains(var)) 401 + while (mainQ.Count > 0)
336 { 402 {
337 - criticalBlock = true; 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;
338 break; 411 break;
339 } 412 }
340 } 413 }
414 + }
415 + mainBuilder.AppendLine(s);
416 + }
341 417
342 - if (!hasRight) 418 + if (mainBuilder.Length > 0)
343 { 419 {
344 - builder.AppendLine(line); 420 + blockList.Add(new Block { Code = mainBuilder.ToString(), HasCritical = cb, Num = crNum++ });
345 -
346 } 421 }
347 - 422 + }
423 + else
424 + {
425 + StringBuilder groupBuilder = new StringBuilder();
426 + while (groupQ.Count > 0)
427 + {
428 + string s = groupQ.Dequeue();
429 + if (!cb)
430 + {
431 + foreach (var item in ExtractCriticalVariant(s))
432 + {
433 + if (crList.Contains(item))
434 + {
435 + cb = true;
436 + break;
437 + }
438 + }
439 + }
440 + groupBuilder.AppendLine(s);
348 } 441 }
349 442
350 - /* 마지막 남은게 있을 수 있으니 추가 */ 443 + if (groupBuilder.Length > 0)
351 - string fs = builder.ToString();
352 - if (!string.IsNullOrWhiteSpace(fs))
353 { 444 {
354 - blockList.Add(new Block() { HasCritical = criticalBlock, Code = fs, Num = blockNum }); 445 + blockList.Add(new Block { Code = groupBuilder.ToString(), HasCritical = cb, Num = crNum++ });
355 - blockNum++; 446 + }
356 - criticalBlock = false;
357 - builder.Clear();
358 } 447 }
359 448
360 return blockList; 449 return blockList;
......
1 +theme: jekyll-theme-midnight
...\ No newline at end of file ...\ No newline at end of file