노현종

GetCriticalBlock 문법 버그 수정

...@@ -127,8 +127,6 @@ namespace VulnCrawler ...@@ -127,8 +127,6 @@ namespace VulnCrawler
127 127
128 using (var reader = new StreamReader(oldStream)) 128 using (var reader = new StreamReader(oldStream))
129 { 129 {
130 -
131 -
132 bool found = false; 130 bool found = false;
133 bool found2 = false; 131 bool found2 = false;
134 bool commentLine = false; 132 bool commentLine = false;
...@@ -142,8 +140,6 @@ namespace VulnCrawler ...@@ -142,8 +140,6 @@ namespace VulnCrawler
142 while (!reader.EndOfStream) 140 while (!reader.EndOfStream)
143 { 141 {
144 string line = reader.ReadLine(); 142 string line = reader.ReadLine();
145 -
146 -
147 if (readCount++ < start) 143 if (readCount++ < start)
148 { 144 {
149 tempQ.Enqueue(line); 145 tempQ.Enqueue(line);
...@@ -157,16 +153,13 @@ namespace VulnCrawler ...@@ -157,16 +153,13 @@ namespace VulnCrawler
157 string method = Regex.Escape(methodName); 153 string method = Regex.Escape(methodName);
158 if (Regex.Match(s, $"{method}").Success) 154 if (Regex.Match(s, $"{method}").Success)
159 { 155 {
160 -
161 break; 156 break;
162 } 157 }
163 } 158 }
164 -
165 while (tempStack.Count > 0) 159 while (tempStack.Count > 0)
166 { 160 {
167 string s = tempStack.Pop(); 161 string s = tempStack.Pop();
168 string trim = s.Trim(); 162 string trim = s.Trim();
169 -
170 if (commentLine) 163 if (commentLine)
171 { 164 {
172 if (Regex.IsMatch(trim, commentPattern3)) 165 if (Regex.IsMatch(trim, commentPattern3))
...@@ -176,27 +169,21 @@ namespace VulnCrawler ...@@ -176,27 +169,21 @@ namespace VulnCrawler
176 } 169 }
177 continue; 170 continue;
178 } 171 }
179 -
180 -
181 string removeString = Regex.Replace(trim, stringPattern, ""); 172 string removeString = Regex.Replace(trim, stringPattern, "");
182 -
183 // /* ~ 패턴 173 // /* ~ 패턴
184 if (Regex.IsMatch(trim, commentPattern2)) 174 if (Regex.IsMatch(trim, commentPattern2))
185 { 175 {
186 -
187 // /* ~ */ 패턴이 아닌 경우 176 // /* ~ */ 패턴이 아닌 경우
188 if (!Regex.IsMatch(trim, commentPattern)) 177 if (!Regex.IsMatch(trim, commentPattern))
189 { 178 {
190 commentLine = true; 179 commentLine = true;
191 } 180 }
192 trim = Regex.Split(trim, "/*")[0]; 181 trim = Regex.Split(trim, "/*")[0];
193 -
194 } 182 }
195 if (string.IsNullOrWhiteSpace(trim)) 183 if (string.IsNullOrWhiteSpace(trim))
196 { 184 {
197 continue; 185 continue;
198 } 186 }
199 -
200 int openBracketCount = removeString.Count(c => c == '{'); 187 int openBracketCount = removeString.Count(c => c == '{');
201 int closeBracketCount = removeString.Count(c => c == '}'); 188 int closeBracketCount = removeString.Count(c => c == '}');
202 int subtract = openBracketCount - closeBracketCount; 189 int subtract = openBracketCount - closeBracketCount;
...@@ -222,10 +209,7 @@ namespace VulnCrawler ...@@ -222,10 +209,7 @@ namespace VulnCrawler
222 } 209 }
223 oldBuilder.AppendLine(s); 210 oldBuilder.AppendLine(s);
224 } 211 }
225 -
226 -
227 } 212 }
228 -
229 } 213 }
230 Console.WriteLine("찾음"); 214 Console.WriteLine("찾음");
231 Console.WriteLine(oldBuilder.ToString()); 215 Console.WriteLine(oldBuilder.ToString());
...@@ -233,8 +217,6 @@ namespace VulnCrawler ...@@ -233,8 +217,6 @@ namespace VulnCrawler
233 217
234 return oldBuilder.ToString(); 218 return oldBuilder.ToString();
235 } 219 }
236 -
237 -
238 public abstract IDictionary<string, IEnumerable<string>> ExtractGitCriticalMethodTable(string srcCode); 220 public abstract IDictionary<string, IEnumerable<string>> ExtractGitCriticalMethodTable(string srcCode);
239 221
240 /// <summary> 222 /// <summary>
......
...@@ -229,9 +229,7 @@ namespace VulnCrawler ...@@ -229,9 +229,7 @@ namespace VulnCrawler
229 229
230 protected override IList<Block> GetCriticalBlocks(string srcCode, IEnumerable<string> criticalList) 230 protected override IList<Block> GetCriticalBlocks(string srcCode, IEnumerable<string> criticalList)
231 { 231 {
232 - // srcCode = Regex.Replace(srcCode, @"if.+\n\{", @"if.+\{", RegexOptions.Multiline); 232 +
233 -
234 - var split = srcCode.Split('\n');
235 var blockList = new List<Block>(); 233 var blockList = new List<Block>();
236 StringBuilder builder = new StringBuilder(); 234 StringBuilder builder = new StringBuilder();
237 var crList = criticalList as HashSet<string>; 235 var crList = criticalList as HashSet<string>;
...@@ -239,34 +237,33 @@ namespace VulnCrawler ...@@ -239,34 +237,33 @@ namespace VulnCrawler
239 { 237 {
240 return null; 238 return null;
241 } 239 }
242 - 240 + var split = srcCode.Split('\n');
243 var mainQ = new Queue<string>(); 241 var mainQ = new Queue<string>();
244 var groupQ = new Queue<string>(); 242 var groupQ = new Queue<string>();
245 bool mainLine = true; 243 bool mainLine = true;
246 int crNum = 1; 244 int crNum = 1;
247 int bracketCount = 1; 245 int bracketCount = 1;
248 bool prevStartBlock = false; 246 bool prevStartBlock = false;
247 + int totalSoBracketCount = 0;
249 foreach (var line in split) 248 foreach (var line in split)
250 { 249 {
250 +
251 bool criticalBlock = false; 251 bool criticalBlock = false;
252 string trimLine = line.Trim(); 252 string trimLine = line.Trim();
253 + if (string.IsNullOrWhiteSpace(trimLine))
254 + {
255 + continue;
256 + }
253 if (mainLine) 257 if (mainLine)
254 { 258 {
255 - if (trimLine.EndsWith("&&") || trimLine.EndsWith("||")) 259 + bracketCount = 1;
256 - { 260 + if (trimLine.StartsWith("else"))
257 - mainQ.Enqueue(line);
258 - continue;
259 - }
260 -
261 - if (trimLine.StartsWith("&&") || trimLine.StartsWith("||"))
262 { 261 {
263 groupQ.Enqueue(line); 262 groupQ.Enqueue(line);
263 + mainLine = false;
264 continue; 264 continue;
265 } 265 }
266 266
267 -
268 -
269 - bracketCount = 1;
270 StringBuilder groupBuilder = new StringBuilder(); 267 StringBuilder groupBuilder = new StringBuilder();
271 while(groupQ.Count > 0) 268 while(groupQ.Count > 0)
272 { 269 {
...@@ -284,15 +281,23 @@ namespace VulnCrawler ...@@ -284,15 +281,23 @@ namespace VulnCrawler
284 } 281 }
285 groupBuilder.AppendLine(s); 282 groupBuilder.AppendLine(s);
286 } 283 }
287 - 284 + if (!string.IsNullOrWhiteSpace(groupBuilder.ToString()))
288 - if (groupBuilder.Length > 0)
289 { 285 {
290 blockList.Add(new Block { Code = groupBuilder.ToString(), HasCritical = criticalBlock, Num = crNum++}); 286 blockList.Add(new Block { Code = groupBuilder.ToString(), HasCritical = criticalBlock, Num = crNum++});
291 } 287 }
292 - if (Regex.IsMatch(trimLine, @"(if|for|while|switch|do)\s*")) 288 +
289 + if (Regex.IsMatch(trimLine, @"^(if|for|while|switch|do)\s*"))
293 { 290 {
291 + /* syntax를 만났을 때 끝에 {가 없으면 */
294 if (!trimLine.EndsWith("{")) 292 if (!trimLine.EndsWith("{"))
295 { 293 {
294 + int soBracketOpenCount = trimLine.Count(c => c == '(');
295 + int soBracketCloseCount = trimLine.Count(c => c == ')');
296 + totalSoBracketCount = (soBracketOpenCount - soBracketCloseCount);
297 + /* if(s()
298 + * && b)
299 + * 이렇게 소괄호가 안맞고 밑 라인에서 이어서 작성하는 경우
300 + */
296 mainLine = false; 301 mainLine = false;
297 prevStartBlock = true; 302 prevStartBlock = true;
298 303
...@@ -312,36 +317,31 @@ namespace VulnCrawler ...@@ -312,36 +317,31 @@ namespace VulnCrawler
312 317
313 continue; 318 continue;
314 } 319 }
320 +
321 +
315 mainQ.Enqueue(line); 322 mainQ.Enqueue(line);
316 } 323 }
317 else 324 else
318 { 325 {
319 - 326 + /* 소괄호 수 세기 */
320 - 327 + int soBracketOpenCount = trimLine.Count(c => c == '(');
328 + int soBracketCloseCount = trimLine.Count(c => c == ')');
321 /* 중괄호 수 세기 */ 329 /* 중괄호 수 세기 */
322 int openBracketCount = trimLine.Count(c => c == '{'); 330 int openBracketCount = trimLine.Count(c => c == '{');
323 int closeBracketCount = trimLine.Count(c => c == '}'); 331 int closeBracketCount = trimLine.Count(c => c == '}');
324 int subtract = openBracketCount - closeBracketCount; 332 int subtract = openBracketCount - closeBracketCount;
325 bracketCount += subtract; 333 bracketCount += subtract;
326 -
327 -
328 - if (trimLine.EndsWith("&&") || trimLine.EndsWith("||"))
329 - {
330 - groupQ.Enqueue(line);
331 - continue;
332 - }
333 -
334 - //if (trimLine.StartsWith("&&") || trimLine.StartsWith("||"))
335 - //{
336 - // mainQ.Enqueue(line);
337 - // continue;
338 -
339 - //}
340 groupQ.Enqueue(line); 334 groupQ.Enqueue(line);
341 if (prevStartBlock) 335 if (prevStartBlock)
342 { 336 {
337 + totalSoBracketCount += (soBracketOpenCount - soBracketCloseCount);
343 prevStartBlock = false; 338 prevStartBlock = false;
344 - if (Regex.IsMatch(trimLine, @"(if|for|while|switch|do)\s*\(")) 339 + if(totalSoBracketCount > 0)
340 + {
341 + prevStartBlock = true;
342 + continue;
343 + }
344 + else if (Regex.IsMatch(trimLine, @"^(if|for|while|switch|do)\s*"))
345 { 345 {
346 prevStartBlock = true; 346 prevStartBlock = true;
347 continue; 347 continue;
...@@ -355,6 +355,15 @@ namespace VulnCrawler ...@@ -355,6 +355,15 @@ namespace VulnCrawler
355 355
356 if (bracketCount <= 1) 356 if (bracketCount <= 1)
357 { 357 {
358 + if (soBracketOpenCount > soBracketCloseCount)
359 + {
360 + continue;
361 + }
362 +
363 + if (!(trimLine.EndsWith("}") || trimLine.EndsWith(";")))
364 + {
365 + continue;
366 + }
358 if (trimLine.Contains("else")) 367 if (trimLine.Contains("else"))
359 { 368 {
360 bracketCount++; 369 bracketCount++;
...@@ -365,12 +374,14 @@ namespace VulnCrawler ...@@ -365,12 +374,14 @@ namespace VulnCrawler
365 mainLine = true; 374 mainLine = true;
366 } 375 }
367 376
377 + /* 메인 라인 블록 추가 */
368 StringBuilder mainBuilder = new StringBuilder(); 378 StringBuilder mainBuilder = new StringBuilder();
369 while (mainQ.Count > 0) 379 while (mainQ.Count > 0)
370 { 380 {
371 string s = mainQ.Dequeue(); 381 string s = mainQ.Dequeue();
372 if (!criticalBlock) 382 if (!criticalBlock)
373 { 383 {
384 + /* 크리티칼 블록 선정 */
374 foreach (var item in ExtractCriticalVariant(s)) 385 foreach (var item in ExtractCriticalVariant(s))
375 { 386 {
376 if (crList.Contains(item)) 387 if (crList.Contains(item))
...@@ -382,10 +393,10 @@ namespace VulnCrawler ...@@ -382,10 +393,10 @@ namespace VulnCrawler
382 } 393 }
383 mainBuilder.AppendLine(s); 394 mainBuilder.AppendLine(s);
384 } 395 }
385 - 396 + string mains = mainBuilder.ToString();
386 - if (mainBuilder.Length > 0) 397 + if (!string.IsNullOrWhiteSpace(mains))
387 { 398 {
388 - blockList.Add(new Block { Code = mainBuilder.ToString(), HasCritical = criticalBlock, Num = crNum++ }); 399 + blockList.Add(new Block { Code = mains, HasCritical = criticalBlock, Num = crNum++ });
389 } 400 }
390 401
391 402
......