Skip to content

[Bug]: NullReferenceException in ResidesInNamespace when using Types(true) #492

Description

@AvremelM

Description

Namespace predicates (ResideInNamespace, ResideInNamespaceMatching) throw a NullReferenceException when the architecture includes function-pointer types, because IType.Namespace is null for them and the extension dereferences it without a guard.

Minimal Working Example

using System.Linq;
using ArchUnitNET.Loader;

var architecture = new ArchLoader()
    .LoadAssemblies(typeof(System.DateTime).Assembly)   // System.Private.CoreLib
    .Build();

// Works fine
Types().That().ResideInNamespace("System").GetObjects(architecture).Count(); 

// Throws NRE
Types(true).That().ResideInNamespace("System").GetObjects(architecture).Count();

Expected Behavior

Types with null namespaces would not match any namespace predicate.

Actual Behavior

Types with null namespaces cause a NullReferenceException

Stack

Object reference not set to an instance of an object.
   at ArchUnitNET.Domain.Extensions.NamingExtensions.FullNameEquals(IHasName cls, String fullName)
   at ArchUnitNET.Domain.Extensions.TypeExtensions.ResidesInNamespace(IType e, String fullName)
   at ArchUnitNET.Fluent.Syntax.Elements.Types.TypePredicatesDefinition`1.<>c__DisplayClass9_0.<ResideInNamespace>b__0(T type)
   at System.Linq.Enumerable.ArrayWhereIterator`1.GetCount(Boolean onlyIfCheap, ReadOnlySpan`1 source, Func`2 predicate)
   at System.Linq.Enumerable.ListWhereIterator`1.GetCount(Boolean onlyIfCheap)

ArchUnitNET Version

0.13.4

.NET Version

.NET 9 (or any CoreCLR version)

Additional Context

32 types with a null Namespace, all Cecil FunctionPointerType, e.g.
method System.Object *(System.IntPtr,System.Object)
method System.Boolean *(System.Object)
They appear only in ReferencedTypes (0 in Types).

Suggested fix:

  • Option 1: Null-guard the namespace extensions, e.g. return e.Namespace?.FullNameMatches(pattern) ?? false; (same for ResidesInNamespace).
  • Option 2: Skip FunctionPointerType when building the type set — ArchBuilder already filters Microsoft.CodeAnalysis.EmbeddedAttribute, System.Runtime.CompilerServices.Nullable(Context)Attribute and Coverlet*.

I'm more than happy to open a PR for either approach (or any other solution) if that helps.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.needs-triageIndicates that an issue needs to be categorized.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions