Skip to content

Fix NullReferenceException - #9225

Merged
MiYanni merged 1 commit into
mainfrom
fix-nre
Aug 5, 2025
Merged

MiYanni merged 1 commit into
mainfrom
fix-nre

Conversation

@drewnoakes

Copy link
Copy Markdown
Member

FirstOrDefault can return null, so it's not safe to dereference unconditionally.

Observed while debugging VS:

System.NullReferenceException: Object reference not set to an instance of an object.

Microsoft.TemplateEngine.Orchestrator.RunnableProjects.LocalizationModelDeserializer.LoadPostActionModels.AnonymousMethod__4_4((System.Collections.Generic.IEnumerable<string> NameParts, string LocalizedString) s)
System.Linq.Enumerable.WhereSelectEnumerableIterator<(System.Collections.Generic.IEnumerable<string>, string), (System.Collections.Generic.IEnumerable<string>, string)>.MoveNext()
System.Linq.Lookup<string, (System.Collections.Generic.IEnumerable<string>, string)>.Create<(System.Collections.Generic.IEnumerable<string>, string)>(System.Collections.Generic.IEnumerable<(System.Collections.Generic.IEnumerable<string>, string)> source, System.Func<(System.Collections.Generic.IEnumerable<string>, string), string> keySelector, System.Func<(System.Collections.Generic.IEnumerable<string>, string), (System.Collections.Generic.IEnumerable<string>, string)> elementSelector, System.Collections.Generic.IEqualityComparer<string> comparer)
System.Linq.GroupedEnumerable<(System.Collections.Generic.IEnumerable<string>, string), string, (System.Collections.Generic.IEnumerable<string>, string)>.GetEnumerator()
Microsoft.TemplateEngine.Orchestrator.RunnableProjects.LocalizationModelDeserializer.LoadManualInstructionModels(System.Collections.Generic.IEnumerable<(System.Collections.Generic.IEnumerable<string> NameParts, string LocalizedString)> strings)
Microsoft.TemplateEngine.Orchestrator.RunnableProjects.LocalizationModelDeserializer.LoadPostActionModels(System.Collections.Generic.List<(string Key, string Value)> localizedStrings)
Microsoft.TemplateEngine.Orchestrator.RunnableProjects.LocalizationModelDeserializer.Deserialize(Microsoft.TemplateEngine.Abstractions.Mount.IFile file)
Microsoft.TemplateEngine.Orchestrator.RunnableProjects.ScannedTemplateInfo.FindLocalizations()
Microsoft.TemplateEngine.Orchestrator.RunnableProjects.ScannedTemplateInfo.ScannedTemplateInfo(Microsoft.TemplateEngine.Abstractions.IEngineEnvironmentSettings settings, Microsoft.TemplateEngine.Abstractions.IGenerator generator, Microsoft.TemplateEngine.Abstractions.Mount.IFile templateFile)
Microsoft.TemplateEngine.Orchestrator.RunnableProjects.RunnableProjectGenerator.GetTemplatesFromMountPointInternalAsync(Microsoft.TemplateEngine.Abstractions.Mount.IMountPoint source, System.Threading.CancellationToken cancellationToken) Microsoft.TemplateEngine.Orchestrator.RunnableProjects.RunnableProjectGenerator.Microsoft.TemplateEngine.Abstractions.IGenerator.GetTemplatesFromMountPointAsync(Microsoft.TemplateEngine.Abstractions.Mount.IMountPoint source, System.Threading.CancellationToken cancellationToken)
Microsoft.TemplateEngine.Edge.Settings.Scanner.ScanMountPointForTemplatesAsync(Microsoft.TemplateEngine.Edge.Settings.Scanner.MountPointScanSource source, bool logValidationResults, bool returnInvalidTemplates, System.Threading.CancellationToken cancellationToken)
Microsoft.TemplateEngine.Edge.Settings.Scanner.ScanAsync(string mountPointUri, System.Threading.CancellationToken cancellationToken)
Microsoft.TemplateEngine.Edge.Settings.TemplatePackageManager.UpdateTemplateCacheAsync.AnonymousMethod__2(int index)

`FirstOrDefault` can return null, so it's not safe to dereference unconditionally.

Observed while debugging VS:

```
System.NullReferenceException: Object reference not set to an instance of an object.

Microsoft.TemplateEngine.Orchestrator.RunnableProjects.LocalizationModelDeserializer.LoadPostActionModels.AnonymousMethod__4_4((System.Collections.Generic.IEnumerable<string> NameParts, string LocalizedString) s)
System.Linq.Enumerable.WhereSelectEnumerableIterator<(System.Collections.Generic.IEnumerable<string>, string), (System.Collections.Generic.IEnumerable<string>, string)>.MoveNext()
System.Linq.Lookup<string, (System.Collections.Generic.IEnumerable<string>, string)>.Create<(System.Collections.Generic.IEnumerable<string>, string)>(System.Collections.Generic.IEnumerable<(System.Collections.Generic.IEnumerable<string>, string)> source, System.Func<(System.Collections.Generic.IEnumerable<string>, string), string> keySelector, System.Func<(System.Collections.Generic.IEnumerable<string>, string), (System.Collections.Generic.IEnumerable<string>, string)> elementSelector, System.Collections.Generic.IEqualityComparer<string> comparer)
System.Linq.GroupedEnumerable<(System.Collections.Generic.IEnumerable<string>, string), string, (System.Collections.Generic.IEnumerable<string>, string)>.GetEnumerator()
Microsoft.TemplateEngine.Orchestrator.RunnableProjects.LocalizationModelDeserializer.LoadManualInstructionModels(System.Collections.Generic.IEnumerable<(System.Collections.Generic.IEnumerable<string> NameParts, string LocalizedString)> strings)
Microsoft.TemplateEngine.Orchestrator.RunnableProjects.LocalizationModelDeserializer.LoadPostActionModels(System.Collections.Generic.List<(string Key, string Value)> localizedStrings)
Microsoft.TemplateEngine.Orchestrator.RunnableProjects.LocalizationModelDeserializer.Deserialize(Microsoft.TemplateEngine.Abstractions.Mount.IFile file)
Microsoft.TemplateEngine.Orchestrator.RunnableProjects.ScannedTemplateInfo.FindLocalizations()
Microsoft.TemplateEngine.Orchestrator.RunnableProjects.ScannedTemplateInfo.ScannedTemplateInfo(Microsoft.TemplateEngine.Abstractions.IEngineEnvironmentSettings settings, Microsoft.TemplateEngine.Abstractions.IGenerator generator, Microsoft.TemplateEngine.Abstractions.Mount.IFile templateFile)
Microsoft.TemplateEngine.Orchestrator.RunnableProjects.RunnableProjectGenerator.GetTemplatesFromMountPointInternalAsync(Microsoft.TemplateEngine.Abstractions.Mount.IMountPoint source, System.Threading.CancellationToken cancellationToken) Microsoft.TemplateEngine.Orchestrator.RunnableProjects.RunnableProjectGenerator.Microsoft.TemplateEngine.Abstractions.IGenerator.GetTemplatesFromMountPointAsync(Microsoft.TemplateEngine.Abstractions.Mount.IMountPoint source, System.Threading.CancellationToken cancellationToken)
Microsoft.TemplateEngine.Edge.Settings.Scanner.ScanMountPointForTemplatesAsync(Microsoft.TemplateEngine.Edge.Settings.Scanner.MountPointScanSource source, bool logValidationResults, bool returnInvalidTemplates, System.Threading.CancellationToken cancellationToken)
Microsoft.TemplateEngine.Edge.Settings.Scanner.ScanAsync(string mountPointUri, System.Threading.CancellationToken cancellationToken)
Microsoft.TemplateEngine.Edge.Settings.TemplatePackageManager.UpdateTemplateCacheAsync.AnonymousMethod__2(int index)
```
@drewnoakes
drewnoakes requested a review from a team as a code owner August 5, 2025 00:34

@joeloff joeloff left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something we should consider backporting for 8 and 9?

@drewnoakes

Copy link
Copy Markdown
Member Author

@joeloff I'm not sure. Seems low risk, but I don't know the impact.

I'm not familiar with the merge policy for this repo. Please merge when makes sense.

@MiYanni
MiYanni merged commit 5c71193 into main Aug 5, 2025
@MiYanni
MiYanni deleted the fix-nre branch August 5, 2025 23:47
mmitche pushed a commit to dotnet/sdk that referenced this pull request Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants