Exemplo n.º 1
0
        public List <AnalysisCheckOutcome> RunCheck(StringBuilder codeToCheckAgainst)
        {
            List <AnalysisCheckOutcome> finalResult = new List <AnalysisCheckOutcome>();

            foreach (RegExCheck currCheck in this.checkRegExs)
            {
                var currMatch = currCheck.RegexToCheck.Match(codeToCheckAgainst.ToString());
                if (currMatch.Success)
                {
                    AnalysisCheckOutcome currOutcome = AnalysisCheckOutcome.CreateInstance(currCheck.ResponseIfCheckPassed);
                    currOutcome.MatchingCodeBlock = new StringBuilder(currMatch.Value);
                    currOutcome.RuleCategory      = this.Category;
                    currOutcome.LineNumber        = this.LineFromPos(codeToCheckAgainst.ToString(), currMatch.Index) + 1;
                    finalResult.Add(currOutcome);
                }
                else
                {
                    AnalysisCheckOutcome currOutcome = AnalysisCheckOutcome.CreateInstance(currCheck.ResponseIfCheckPassed);
                    currOutcome.MatchingCodeBlock = new StringBuilder(string.Empty);
                    currOutcome.Level             = OutcomeLevels.NoMatch;
                    currOutcome.RuleCategory      = this.Category;
                    finalResult.Add(currOutcome);
                }
            }
            return(finalResult);
        }
Exemplo n.º 2
0
        public AnalysisCheckOutcomeResponse Post([FromBody] string codeString)
        {
            if (string.IsNullOrWhiteSpace(codeString))
            {
                throw new ArgumentNullException("codeString", "Unable to run problem detection analysis on empty code block");
            }
            StringBuilder codeSb = new StringBuilder(string.Empty);

            string[] codeLines = codeString.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            foreach (string codeLine in codeLines)
            {
                codeSb.AppendLine(codeLine);
            }

            Regex infiniteWhileLoopRegEx = new Regex(@"[\{|\}]*\s*[while]+\s*[\(]\s*[true|1|\!false|\!0]+\s*[\)]", RegexOptions.IgnoreCase);
            AnalysisCheckOutcome outcomeForInfiniteWhileLoop = new AnalysisCheckOutcome(OutcomeLevels.Warning, "Potential infinite while loop", "Infinite while loops can cause CPU spike if the condition to break the loop fails.", "Consider assigning a max iteration to the while loop or an alternative bounding condition and deterministically terminate the loop.");
            RegExCheck           infiniteWhileLoop           = new RegExCheck(infiniteWhileLoopRegEx, outcomeForInfiniteWhileLoop);


            RegexCodeCheck infiniteLoopCheck = new RegexCodeCheck(RuleCategories.HighCpu, "Infinite Loop Check", new List <RegExCheck>()
            {
                infiniteWhileLoop
            });
            List <AnalysisCheckOutcome> infiniteLooEvalResults = infiniteLoopCheck.RunCheck(codeSb);

            if (infiniteLoopCheck.isMatch(infiniteLooEvalResults))
            {
                AnalysisCheckOutcomeResponse response = new AnalysisCheckOutcomeResponse();
                response.RuleName             = infiniteLoopCheck.RuleName;
                response.RuleCategory         = infiniteLoopCheck.Category;
                response.AnalysisCheckResults = new List <AnalysisCheckOutcome>();
                foreach (AnalysisCheckOutcome evalResult in infiniteLoopCheck.GetAssertedOutcomes(infiniteLooEvalResults))
                {
                    evalResult.MatchingCodeBlock = new StringBuilder(evalResult.MatchingCodeBlock.ToString().Trim().Replace(@"\t", string.Empty).Replace(@"\n", string.Empty));
                    response.AnalysisCheckResults.Add(evalResult);
                }
                return(response);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 3
0
 public RegExCheck(Regex regexTocheck, AnalysisCheckOutcome outcomeOnMatch)
 {
     this.RegexToCheck          = regexTocheck ?? throw new ArgumentNullException("regexTocheck", "Failed to create RegExCheck due to NULL parameter");
     this.ResponseIfCheckPassed = outcomeOnMatch ?? throw new ArgumentNullException("outcomeOnMatch", "Failed to create RegExCheck due to NULL parameter");
 }