From f8ace6ad4e18ff869563cfbad8efba95436e395e Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Tue, 5 Sep 2023 21:19:34 +0200 Subject: [PATCH] C#: Clear TRAP stack when calling `PopulateGenerics` --- .../Entities/Types/Type.cs | 55 ++++++++++--------- csharp/extractor/Semmle.Extraction/Context.cs | 4 +- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs index 909b255871fd5..4f51ae74c936c 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs @@ -250,41 +250,44 @@ public void ExtractRecursive() /// public void PopulateGenerics() { - if (Symbol is null || !NeedsPopulation || !Context.ExtractGenerics(this)) - return; + Context.PopulateLater(() => + { + if (Symbol is null || !NeedsPopulation || !Context.ExtractGenerics(this)) + return; - var members = new List(); + var members = new List(); - foreach (var member in Symbol.GetMembers()) - members.Add(member); - foreach (var member in Symbol.GetTypeMembers()) - members.Add(member); + foreach (var member in Symbol.GetMembers()) + members.Add(member); + foreach (var member in Symbol.GetTypeMembers()) + members.Add(member); - // Mono extractor puts all BASE interface members as members of the current interface. + // Mono extractor puts all BASE interface members as members of the current interface. - if (Symbol.TypeKind == TypeKind.Interface) - { - foreach (var baseInterface in Symbol.Interfaces) + if (Symbol.TypeKind == TypeKind.Interface) { - foreach (var member in baseInterface.GetMembers()) - members.Add(member); - foreach (var member in baseInterface.GetTypeMembers()) - members.Add(member); + foreach (var baseInterface in Symbol.Interfaces) + { + foreach (var member in baseInterface.GetMembers()) + members.Add(member); + foreach (var member in baseInterface.GetTypeMembers()) + members.Add(member); + } } - } - foreach (var member in members) - { - Context.CreateEntity(member); - } + foreach (var member in members) + { + Context.CreateEntity(member); + } - if (Symbol.BaseType is not null) - Create(Context, Symbol.BaseType).PopulateGenerics(); + if (Symbol.BaseType is not null) + Create(Context, Symbol.BaseType).PopulateGenerics(); - foreach (var i in Symbol.Interfaces) - { - Create(Context, i).PopulateGenerics(); - } + foreach (var i in Symbol.Interfaces) + { + Create(Context, i).PopulateGenerics(); + } + }, preserveDuplicationKey: false); } public void ExtractRecursive(TextWriter trapFile, IEntity parent) diff --git a/csharp/extractor/Semmle.Extraction/Context.cs b/csharp/extractor/Semmle.Extraction/Context.cs index 6694757f0d837..a6442758daf23 100644 --- a/csharp/extractor/Semmle.Extraction/Context.cs +++ b/csharp/extractor/Semmle.Extraction/Context.cs @@ -153,9 +153,9 @@ internal void AddFreshLabel(Entity entity) /// Enqueue the given action to be performed later. /// /// The action to run. - public void PopulateLater(Action a) + public void PopulateLater(Action a, bool preserveDuplicationKey = true) { - var key = GetCurrentTagStackKey(); + var key = preserveDuplicationKey ? GetCurrentTagStackKey() : null; if (key is not null) { // If we are currently executing with a duplication guard, then the same