Skip to content

Commit

Permalink
fix crash on invalid cref
Browse files Browse the repository at this point in the history
  • Loading branch information
saucecontrol committed Oct 12, 2024
1 parent 44650b7 commit 9e66e34
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/InheritDoc/InheritDoc.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>2.0.1</VersionPrefix>
<VersionPrefix>2.0.2</VersionPrefix>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>

Expand All @@ -21,14 +21,14 @@

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.0.461" />
<PackageReference Include="Mono.Cecil" Version="0.11.5" PrivateAssets="all" />
<PackageReference Include="Mono.Cecil" Version="0.11.6" PrivateAssets="all" />
<!-- these are for use in the test project, downloaded here to avoid version conflicts when building tests -->
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[7.0.0]" />
<PackageDownload Include="Microsoft.NETFramework.ReferenceAssemblies.net48" Version="[1.0.0]" />
</ItemGroup>

<Target Name="VerifyPackDependencies" BeforeTargets="GenerateNuspec">
<Error Condition="@(None->WithMetadataValue('Pack', 'true')->WithMetadataValue('Filename', 'Mono.Cecil')->Count()) != 1" Text="Dependencies were not present for pack. Start build again." />
<Error Condition="@(None-&gt;WithMetadataValue('Pack', 'true')-&gt;WithMetadataValue('Filename', 'Mono.Cecil')-&gt;Count()) != 1" Text="Dependencies were not present for pack. Start build again." />
</Target>

</Project>
8 changes: 5 additions & 3 deletions src/InheritDoc/InheritDocProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static XDocument loadDoc(string path)
;

var docMap = generateDocMap(types, docMembers, trimLevel, logger);
var docCref = docMembers.Elements(DocElementNames.Member).Where(m => !m.HasAttribute(DocAttributeNames._trimmed)).Descendants(DocElementNames.InheritDoc).Select(i => (string)i.Attribute(DocAttributeNames.Cref)).Where(c => !string.IsNullOrWhiteSpace(c));
var docCref = docMembers.Elements(DocElementNames.Member).Where(m => !m.HasAttribute(DocAttributeNames._trimmed)).Descendants(DocElementNames.InheritDoc).Select(i => (string)i.Attribute(DocAttributeNames.Cref)).Where(isValidCref);
var asmTypes = types.Select(t => t.GetDocID()).ToHashSet();

var refCref = docMap.Values.SelectMany(v => v.Select(l => l.Cref)).Concat(docCref).Where(c => !asmTypes.Contains(getTypeIDFromDocID(c))).ToHashSet();
Expand Down Expand Up @@ -171,7 +171,7 @@ private static IDictionary<string, IEnumerable<DocMatch>> generateDocMap(IList<T
{
logger.Write(ILogger.Severity.Diag, "Processing DocID: " + typeID);

var crefs = typeDocs.Descendants(DocElementNames.InheritDoc).Select(i => (string)i.Attribute(DocAttributeNames.Cref)).Where(c => !string.IsNullOrWhiteSpace(c)).ToList();
var crefs = typeDocs.Descendants(DocElementNames.InheritDoc).Select(i => (string)i.Attribute(DocAttributeNames.Cref)).Where(isValidCref).ToList();
var dml = new List<DocMatch>();
docMap.Add(typeID, dml);

Expand Down Expand Up @@ -267,7 +267,7 @@ private static IDictionary<string, IEnumerable<DocMatch>> generateDocMap(IList<T
logger.Write(ILogger.Severity.Diag, "Processing DocID: " + memID);

var bases = (om is not null ? (new[] { om }) : []).Concat(m.GetBaseCandidates()).ToList();
var crefs = methDocs.Descendants(DocElementNames.InheritDoc).Select(i => (string)i.Attribute(DocAttributeNames.Cref)).Where(c => !string.IsNullOrWhiteSpace(c)).ToHashSet();
var crefs = methDocs.Descendants(DocElementNames.InheritDoc).Select(i => (string)i.Attribute(DocAttributeNames.Cref)).Where(isValidCref).ToHashSet();
var dml = new List<DocMatch>();

foreach (var (bm, cref) in bases.SelectMany(bm => bm.GetDocID().Select(d => (bm, d))))
Expand Down Expand Up @@ -555,6 +555,8 @@ static XDocument getRefDocs(IReadOnlyCollection<string> refAssemblies, IReadOnly
}
}

private static bool isValidCref(string cref) => cref is { Length: > 2 } && cref[0] is not '!' && (cref[0] is 'T' || cref.IndexOf('.', 2) > 0);

private static string getTypeIDFromDocID(string docID)
{
if (docID[0] == 'T')
Expand Down
6 changes: 3 additions & 3 deletions tests/InheritDoc.Test/InheritDoc.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 9e66e34

Please sign in to comment.