From d34b7afa1371a4e113cd0e7768ba0d8e894d7123 Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Thu, 17 Sep 2026 19:01:49 +0100 Subject: [PATCH] C#: Prevent bad magic in 'getAnAccess'. --- csharp/ql/lib/semmle/code/csharp/Assignable.qll | 2 ++ csharp/ql/lib/semmle/code/csharp/Member.qll | 1 + csharp/ql/lib/semmle/code/csharp/Property.qll | 2 ++ csharp/ql/lib/semmle/code/csharp/Variable.qll | 2 ++ 4 files changed, 7 insertions(+) diff --git a/csharp/ql/lib/semmle/code/csharp/Assignable.qll b/csharp/ql/lib/semmle/code/csharp/Assignable.qll index 89dc594ec3f4..d98d78331c83 100644 --- a/csharp/ql/lib/semmle/code/csharp/Assignable.qll +++ b/csharp/ql/lib/semmle/code/csharp/Assignable.qll @@ -18,6 +18,7 @@ class Assignable extends Declaration, @assignable { final AnnotatedType getAnnotatedType() { result.appliesTo(this) } /** Gets an access to this assignable. */ + pragma[nomagic] AssignableAccess getAnAccess() { result.getTarget() = this } /** Gets an expression assigned to this assignable, if any. */ @@ -31,6 +32,7 @@ class Assignable extends Declaration, @assignable { * property (`Property`), an indexer (`Indexer`), or an event (`Event`). */ class AssignableMember extends Member, Assignable, Attributable { + pragma[nomagic] override AssignableMemberAccess getAnAccess() { result = Assignable.super.getAnAccess() } override string toString() { result = Assignable.super.toString() } diff --git a/csharp/ql/lib/semmle/code/csharp/Member.qll b/csharp/ql/lib/semmle/code/csharp/Member.qll index b64f408af64c..ac3229351ea3 100644 --- a/csharp/ql/lib/semmle/code/csharp/Member.qll +++ b/csharp/ql/lib/semmle/code/csharp/Member.qll @@ -238,6 +238,7 @@ class Modifiable extends Declaration, @modifiable { /** A declaration that is a member of a type. */ class Member extends Modifiable, @member { /** Gets an access to this member. */ + pragma[nomagic] MemberAccess getAnAccess() { result.getTarget() = this } /** diff --git a/csharp/ql/lib/semmle/code/csharp/Property.qll b/csharp/ql/lib/semmle/code/csharp/Property.qll index 3a007b0d6e9d..7c048528aaae 100644 --- a/csharp/ql/lib/semmle/code/csharp/Property.qll +++ b/csharp/ql/lib/semmle/code/csharp/Property.qll @@ -217,6 +217,7 @@ class Property extends DeclarationWithGetSetAccessors, @property { result = DeclarationWithGetSetAccessors.super.getAnUltimateImplementor() } + pragma[nomagic] override PropertyAccess getAnAccess() { result.getTarget() = this } override Location getALocation() { property_location(this.getUnboundDeclaration(), result) } @@ -326,6 +327,7 @@ class Indexer extends DeclarationWithGetSetAccessors, Parameterizable, @indexer indexers(this, _, _, getTypeRef(result), _) } + pragma[nomagic] override IndexerAccess getAnAccess() { result.getTarget() = this } /** diff --git a/csharp/ql/lib/semmle/code/csharp/Variable.qll b/csharp/ql/lib/semmle/code/csharp/Variable.qll index 2d4cf578436d..b8e3ae0190d6 100644 --- a/csharp/ql/lib/semmle/code/csharp/Variable.qll +++ b/csharp/ql/lib/semmle/code/csharp/Variable.qll @@ -16,6 +16,7 @@ private import TypeRef class Variable extends Assignable, @variable { override Variable getUnboundDeclaration() { result = this } + pragma[nomagic] override VariableAccess getAnAccess() { result.getTarget() = this } /** Gets the type of this variable. */ @@ -438,6 +439,7 @@ class Field extends Variable, AssignableMember, Attributable, TopLevelExprParent override Field getUnboundDeclaration() { fields(this, _, _, _, _, result) } + pragma[nomagic] override FieldAccess getAnAccess() { result = Variable.super.getAnAccess() } override ValueOrRefType getDeclaringType() { fields(this, _, _, result, _, _) }