Repository navigation
Closed
Labels
Description
Problem
GenerateNativeAotEnvironmentAssemblerSources.cs lacks #nullable enable and has potential null-dereference bugs: outputFile is declared as ITaskItem? but dereferenced without a null check on line 32, and the ! null-forgiving operator is used on lines 40 and 42 (which is prohibited by repo conventions).
Location
- File:
src/Xamarin.Android.Build.Tasks/Tasks/GenerateNativeAotEnvironmentAssemblerSources.cs - Lines: 31–42
Current Code
ITaskItem? outputFile = GenerateNativeAotLibraryLoadAssemblerSources.FindOutputFile (OutputSources, abi: abi, rid: RID);
Log.LogDebugMessage ($"Environment variables file to generate: {outputFile.ItemSpec}");
string environmentLlFilePath = outputFile.ItemSpec;
var generator = new NativeAotEnvironmentNativeAssemblyGenerator (Log, envBuilder);
LLVMIR.LlvmIrModule environmentModule = generator.Construct ();
using var environmentWriter = MemoryStreamPool.Shared.CreateStreamWriter ();
bool fileFullyWritten = false;
try {
generator.Generate (environmentModule, targetArch, environmentWriter, environmentLlFilePath!);
environmentWriter.Flush ();
Files.CopyIfStreamChanged (environmentWriter.BaseStream, environmentLlFilePath!);Suggested Fix
-
Add
#nullable enableat the very top of the file (no preceding blank lines). -
Add a null check after
FindOutputFilereturns, and throw if null — this replaces the implicit dereference on line 32:
ITaskItem? outputFile = GenerateNativeAotLibraryLoadAssemblerSources.FindOutputFile (OutputSources, abi: abi, rid: RID);
if (outputFile == null) {
Log.LogCodedError ("XA0000", Properties.Resources.XA0000, $"Could not find output file for ABI '{abi}' and RID '{RID}'");
return false;
}
Log.LogDebugMessage ($"Environment variables file to generate: {outputFile.ItemSpec}");Alternatively, if no specific error code exists for this scenario, a simpler approach is fine:
if (outputFile == null) {
Log.LogError ($"Could not find output file for ABI '{abi}' and RID '{RID}'");
return false;
}- Remove the
!null-forgiving operators on lines 40 and 42. After the null check above,environmentLlFilePathis guaranteed non-null, so the!is unnecessary:
generator.Generate (environmentModule, targetArch, environmentWriter, environmentLlFilePath);
environmentWriter.Flush ();
Files.CopyIfStreamChanged (environmentWriter.BaseStream, environmentLlFilePath);- TFM note: This project targets
netstandard2.0— do NOT useArgumentNullException.ThrowIfNull. Use explicitif+throworLog.LogError+return falsepatterns instead.
Guidelines
- Follow repo Mono formatting style (tabs, space before parentheses)
- Never use
!(null-forgiving operator) - For
netstandard2.0projects, use explicit null checks, notArgumentNullException.ThrowIfNull [Required]properties should be non-nullable with defaults (already correct in this file)- Non-required properties should be nullable (already correct for
Environments)
Acceptance Criteria
-
#nullable enableadded at the top of the file - Null check added for
outputFilewith appropriate error logging -
!null-forgiving operators removed from lines 40 and 42 - No new warnings introduced
- All tests pass
Fix-finder metadata
- Script:
01-nullable-reference-types - Score:
26/30(actionability: 9, safety: 8, scope: 9)
Generated by Nightly Fix Finder · ● 7.3M · ◷
- expires on Jun 7, 2026, 2:29 AM UTC
Reacted by Copilot
Activity