Skip to content

New PdbDocument has null CustomDebugInfos, so writing a Portable PDB throws NullReferenceException #584

Open

Description

@pcshrosbree

A PdbDocument created with new PdbDocument() or with the six-argument constructor has CustomDebugInfos == null. Only PdbDocument(SymbolDocument) creates the list. If a module's PdbState contains such a document, writing the module with a Portable PDB throws NullReferenceException. Calling doc.CustomDebugInfos.Add(...) on such a document throws too.

Repro:

using System;
using dnlib.DotNet;
using dnlib.DotNet.Pdb;
using dnlib.DotNet.Writer;

var mod = new ModuleDefUser("Repro.dll") { Kind = ModuleKind.Dll };
new AssemblyDefUser("Repro", new Version(1, 0, 0, 0)).Modules.Add(mod);
mod.CreatePdbState(PdbFileKind.PortablePDB);

var doc = new PdbDocument("/src/C.cs", PdbDocumentConstants.LanguageCSharp,
	PdbDocumentConstants.LanguageVendorMicrosoft, PdbDocumentConstants.DocumentTypeText,
	PdbDocumentConstants.HashSHA256, new byte[32]);
Console.WriteLine(doc.CustomDebugInfos is null); // True
mod.PdbState.Add(doc);

mod.Write("Repro.dll", new ModuleWriterOptions(mod) { WritePdb = true }); // NullReferenceException

Actual (first frames, paths shortened):

True
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at dnlib.DotNet.Writer.Metadata.AddCustomDebugInformationList(Table table, UInt32 rid, IList`1 cdis) in src/DotNet/Writer/Metadata.cs:line 3367
   at dnlib.DotNet.Writer.Metadata.AddPdbDocument(PdbDocument doc) in src/DotNet/Writer/Metadata.cs:line 3498
   at dnlib.DotNet.Writer.Metadata.AddPdbDocuments() in src/DotNet/Writer/Metadata.cs:line 3481

Expected: a new document has an empty, non-null CustomDebugInfos list, and the module and its PDB are written.

I reproduced this on master (9ab9b58), using dnlib's net6.0 build with .NET SDK 10.0.401 on Linux x64. The files involved are unchanged since v4.5.0.

Cause: commit 628db9b ("Support embedded sources, source link, source server") removed the customDebugInfos field initializer and creates the list in Initialize(SymbolDocument) instead. The other two public constructors never call Initialize, so their documents keep a null list.

I noticed this while reviewing #580, where I offered to report it separately. I'll open a PR with a fix.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions