From 7a001f4905a1a81d2c3c4cff6685e2581f08ef2d Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Wed, 15 Nov 2023 10:55:58 +0100 Subject: [PATCH] C#: Fix assembly attribute extraction in standalone mode --- .../Populators/TypeContainerVisitor.cs | 4 +++- .../standalone/assemblyattribute/attr.expected | 2 ++ .../standalone/assemblyattribute/attr.ql | 5 +++++ .../standalone/assemblyattribute/options | 1 + .../standalone/assemblyattribute/standalone.cs | 12 ++++++++++++ 5 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 csharp/ql/test/library-tests/standalone/assemblyattribute/attr.expected create mode 100644 csharp/ql/test/library-tests/standalone/assemblyattribute/attr.ql create mode 100644 csharp/ql/test/library-tests/standalone/assemblyattribute/options create mode 100644 csharp/ql/test/library-tests/standalone/assemblyattribute/standalone.cs diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs index 2bd5a9f4e031..3a2caff6102c 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs @@ -89,8 +89,10 @@ public override void VisitAttributeList(AttributeListSyntax node) SyntaxKind.ModuleKeyword => Entities.AttributeKind.Module, _ => throw new InternalError(node, "Unhandled global target") }; - foreach (var attribute in node.Attributes) + var attributes = node.Attributes; + for (var i = 0; i < attributes.Count; i++) { + var attribute = attributes[i]; if (attributeLookup.Value(attribute) is AttributeData attributeData) { var ae = Entities.Attribute.Create(Cx, attributeData, outputAssembly, kind); diff --git a/csharp/ql/test/library-tests/standalone/assemblyattribute/attr.expected b/csharp/ql/test/library-tests/standalone/assemblyattribute/attr.expected new file mode 100644 index 000000000000..a8e2f7d8aeb7 --- /dev/null +++ b/csharp/ql/test/library-tests/standalone/assemblyattribute/attr.expected @@ -0,0 +1,2 @@ +| standalone.cs:3:12:3:29 | [assembly: Attribute1(...)] | +| standalone.cs:9:2:9:11 | [Attribute1(...)] | diff --git a/csharp/ql/test/library-tests/standalone/assemblyattribute/attr.ql b/csharp/ql/test/library-tests/standalone/assemblyattribute/attr.ql new file mode 100644 index 000000000000..f1f82ed8f785 --- /dev/null +++ b/csharp/ql/test/library-tests/standalone/assemblyattribute/attr.ql @@ -0,0 +1,5 @@ +import csharp + +from Attribute a +where a.getType().getName() = "Attribute1Attribute" +select a diff --git a/csharp/ql/test/library-tests/standalone/assemblyattribute/options b/csharp/ql/test/library-tests/standalone/assemblyattribute/options new file mode 100644 index 000000000000..7ba3811b2afb --- /dev/null +++ b/csharp/ql/test/library-tests/standalone/assemblyattribute/options @@ -0,0 +1 @@ +semmle-extractor-options: --standalone diff --git a/csharp/ql/test/library-tests/standalone/assemblyattribute/standalone.cs b/csharp/ql/test/library-tests/standalone/assemblyattribute/standalone.cs new file mode 100644 index 000000000000..a03550d040e0 --- /dev/null +++ b/csharp/ql/test/library-tests/standalone/assemblyattribute/standalone.cs @@ -0,0 +1,12 @@ +using System; + +[assembly: global::Attribute1] + +class Attribute1Attribute : Attribute +{ +} + +[Attribute1] +class A +{ +}