Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
PreferTryParseRule.cs : fixed null-reference exception that happens w…
Browse files Browse the repository at this point in the history
…hen Resolve() method cannot find a library with base type and returns null

PreferUriOverStringRule.cs : fixed index-out-of-range exception that happens for three-letter method names (test is included)
AvoidDeepInheritanceTreeRule.cs : fixed null-reference exception that happens when Resolve() method cannot find a library with base type and returns null
  • Loading branch information
usarskyy committed Mar 7, 2012
1 parent b9a1a4b commit 2d9fc5f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
Expand Up @@ -118,6 +118,7 @@ public class PreferTryParseRule : Rule, IMethodRule {
static bool HasTryParseMethod (TypeDefinition type)
{
bool present = false;

if (!has_try_parse.TryGetValue (type, out present)) {
foreach (MethodReference method in type.Methods) {
if (MethodSignatures.TryParse.Matches (method)) {
Expand Down Expand Up @@ -162,7 +163,8 @@ public RuleResult CheckMethod (MethodDefinition method)
if (!MethodSignatures.Parse.Matches (mr))
continue;

if (!HasTryParseMethod (mr.DeclaringType.Resolve ()))
TypeDefinition declaringType = mr.DeclaringType.Resolve();
if (declaringType != null && !HasTryParseMethod(declaringType))
continue;

// if inside a try (catch/finally) block then...
Expand Down
Expand Up @@ -111,10 +111,11 @@ private static int FindTokenStart (string memberName, string token, int start)
private static bool IsUri (string memberName)
{
int index = 0;
while ((index = FindTokenStart (memberName, "ur", index)) != -1) {
if (memberName.Length < index + 2)
while ((index = FindTokenStart(memberName, "ur", index)) != -1)
{
if (memberName.Length <= index + 2)
break;
if (url_enders.Contains (Char.ToLower (memberName [index + 2], CultureInfo.InvariantCulture)))
if (url_enders.Contains(Char.ToLower(memberName[index + 2], CultureInfo.InvariantCulture)))
return true;
index += 2;
}
Expand Down
Expand Up @@ -52,6 +52,11 @@ public string Urn
get;
set;
}
public string CUR
{
get;
set;
}

public Uri GetUri ()
{
Expand Down Expand Up @@ -117,6 +122,7 @@ public void Good ()
{
AssertRuleSuccess (SimpleMethods.EmptyMethod);
AssertRuleSuccess<GoodUris> ("GetUri");
AssertRuleSuccess<GoodUris>("get_CUR");
AssertRuleSuccess<GoodUris> ("GetNewLink");
AssertRuleSuccess<GoodUris> ("OverloadedMethod", new Type [] { typeof (string) });
}
Expand Down
Expand Up @@ -85,11 +85,13 @@ public RuleResult CheckType (TypeDefinition type)
return RuleResult.DoesNotApply;

int depth = 0;
while (type.BaseType != null) {
type = type.BaseType.Resolve ();
if (type == null)
TypeDefinition temp = type;
while (temp.BaseType != null)
{
temp = temp.BaseType.Resolve();
if (temp == null)
break;
if (countExternalDepth || Runner.Assemblies.Contains (type.Module.Assembly))
if (countExternalDepth || Runner.Assemblies.Contains(temp.Module.Assembly))
depth++;
}

Expand Down

0 comments on commit 2d9fc5f

Please sign in to comment.