镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

A null DefaultValue with DefaultValueIsQuoted = true throws NullReferenceException mid-declaration and leaves void M} #209

Open

Description

@matt-edmondson

What's wrong

When DefaultValueIsQuoted is set, TemplateBase.HasDefaultValue (CodeBlocker/Templates/TemplateBase.cs:41) is true whatever the value, so WriteDefaultValueTo (:108-119) calls TemplateRendering.QuoteStringLiteral(DefaultValue). That method dereferences value.Length at TemplateRendering.cs:144, so a null value throws a NullReferenceException. By then the declaration is already partly written.

An unquoted null DefaultValue is accepted and treated as "no initialiser". The quoted path is the only one that crashes.

Repro (HEAD 032f69a)

var m = new MethodTemplate { Name = "M", Type = "void", BodyFactory = ... };
m.Parameters.Add(new ParameterTemplate { Name = "s", Type = "string", DefaultValue = null!, DefaultValueIsQuoted = true });
c.Members.Add(m);
cb.AddClass(c);
NullReferenceException: Object reference not set to an instance of an object.
class C
{
	void M}

A field or property with a quoted null default fails the same way.

Why it matters

A generator that maps an optional string from data, for example DefaultValue = attribute?.Value, DefaultValueIsQuoted = true, gets a bare NullReferenceException and a corrupt half-written class. It gets neither "" nor an up-front refusal. #198/#199 fixed this same symptom for null parameters and members, and DefaultValue's docs already say an empty quoted value writes "".

Suggested fix / acceptance criteria

Activity

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

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions