Skip to content

Commit

Permalink
Use DecodeValue to check attribute values (#17605)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellahathaway authored Oct 26, 2023
1 parent f66cdff commit 313186d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ public class CheckForPoison : Task

private const string SbrpAttributeType = "System.Reflection.AssemblyMetadataAttribute";

private const string SbrpAttributeValuePattern = "source\\s?source\\-build\\-reference\\-packages";

private record CandidateFileEntry(string ExtractedPath, string DisplayPath);

public override bool Execute()
Expand Down Expand Up @@ -370,11 +368,17 @@ private static bool IsAttributeSbrp(MetadataReader reader, CustomAttribute attr)

if (attributeType == SbrpAttributeType)
{
BlobReader blobReader = reader.GetBlobReader(attr.Value);
string attributeValue = Encoding.UTF8.GetString(blobReader.ReadBytes(blobReader.Length));
attributeValue = Regex.Replace(attributeValue, @"\p{C}+", string.Empty);
return Regex.IsMatch(attributeValue, SbrpAttributeValuePattern);
var decodedValue = attr.DecodeValue(DummyAttributeTypeProvider.Instance);
try
{
return decodedValue.FixedArguments[0].Value.ToString() == "source" && decodedValue.FixedArguments[1].Value.ToString() == "source-build-reference-packages";
}
catch
{
throw new InvalidOperationException($"{SbrpAttributeType} is not formatted properly with a key, value pair.");
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;

namespace Microsoft.DotNet.SourceBuild.Tasks.LeakDetection
{

// An empty ICustomAttributeTypeProvider implementation is necessary to read metadata attribute values.
internal class DummyAttributeTypeProvider : ICustomAttributeTypeProvider<Type>
{
public static readonly DummyAttributeTypeProvider Instance = new();

public Type GetPrimitiveType(PrimitiveTypeCode typeCode) => default(Type);

public Type GetSystemType() => default(Type);

public Type GetSZArrayType(Type elementType) => default(Type);

public Type GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind) => default(Type);

public Type GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) => default(Type);

public Type GetTypeFromSerializedName(string name) => default(Type);

public PrimitiveTypeCode GetUnderlyingEnumType(Type type) => default(PrimitiveTypeCode);

public bool IsSystemType(Type type) => default(bool);
}
}

0 comments on commit 313186d

Please sign in to comment.