Examples of errors detected by the V3146 diagnostic
V3146. Possible null dereference. A method can return default null value.
Azure SDK for .NET
V3146 Possible null dereference of 'value'. The 'FirstOrDefault' can return default null value. BatchSharedKeyCredential.cs 127
public override Task
ProcessHttpRequestAsync(HttpRequestMessage httpRequest, ....)
{
....
foreach (string canonicalHeader in customHeaders)
{
string value = httpRequest.Headers.
GetValues(canonicalHeader).FirstOrDefault();
value = value.Replace('\n', ' ').Replace('\r', ' ').TrimStart();
....
}
....
}
osu!
V3146 [CWE-476] Possible null dereference of 'ruleset'. The 'FirstOrDefault' can return default null value. APILegacyScoreInfo.cs 24
public ScoreInfo CreateScoreInfo(RulesetStore rulesets)
{
var ruleset = rulesets.GetRuleset(OnlineRulesetID);
var mods = Mods != null ? ruleset.CreateInstance()
.GetAllMods().Where(....)
.ToArray() : Array.Empty<Mod>();
....
}
public RulesetInfo GetRuleset(int id) =>
AvailableRulesets.FirstOrDefault(....);
RavenDB
V3146 Possible null dereference of 'mainSchema'. The 'Tables.FirstOrDefault' can return default null value. DatabaseSchema.cs(31) Raven.Server
public HashSet<string> FindSpecialColumns(string tableSchema, string tableName)
{
var mainSchema = GetTable(tableSchema, tableName);
var result = new HashSet<string>();
mainSchema.PrimaryKeyColumns.ForEach(x => result.Add(x)); // <=
foreach (var fkCandidate in Tables)
foreach (var tableReference in fkCandidate.References.Where(
x => x.Table == tableName && x.Schema == tableSchema
)
)
{
tableReference.Columns.ForEach(x => result.Add(x));
}
return result;
}
public TableSchema GetTable(string schema, string tableName)
{
return Tables.FirstOrDefault(
x => x.Schema == schema && x.TableName == tableName
);
}
OpenRA
V3146 Possible null dereference of 'tooltip'. The 'FirstOrDefault' can return default null value. Disguise.cs 192
public void DisguiseAs(Actor target)
{
....
var tooltip = target.TraitsImplementing<ITooltip>().FirstOrDefault();
AsPlayer = tooltip.Owner;
AsActor = target.Info;
AsTooltipInfo = tooltip.TooltipInfo;
....
}
ONLYOFFICE Community Server
V3146 Possible null dereference. The 'listTags.FirstOrDefault' can return default null value. FileMarker.cs 299
public static void RemoveMarkAsNew(....)
{
....
var listTags = tagDao.GetNewTags(userID, (Folder)fileEntry, true).ToList();
valueNew = listTags.FirstOrDefault(tag => tag.EntryId.Equals(....)).Count;
....
}
Similar errors can be found in some other places:
- V3146 Possible null dereference. The 'FirstOrDefault' can return default null value. CalendarApi.cs 335
ILSpy
V3146 Possible null dereference. The 'FirstOrDefault' can return default null value. ILSpy.BamlDecompiler BamlResourceEntryNode.cs 76
bool LoadBaml(AvalonEditTextOutput output, CancellationToken cancellationToken)
{
var asm = this.Ancestors().OfType<AssemblyTreeNode>()
.FirstOrDefault().LoadedAssembly; // <=
....
return true;
}
PascalABC.NET
V3146 [CWE-476] Possible null dereference. The 'symbolInfo.FirstOrDefault()' can return default null value. SystemLibInitializer.cs 112
public class SymbolInfo
{
....
}
....
List<TreeConverter.SymbolInfo> symbolInfo = null;
....
public List<TreeConverter.SymbolInfo> SymbolInfo
{
get
{
if (symbolInfo != null && ....)
{
if (symbolInfo.FirstOrDefault().sym_info is common_type_node) // <=
....
}
}
}
MonoGame
V3146 Possible null dereference of 'fi'. The 'FirstOrDefault' can return default null value. MonoGame.Content.Builder CommandLineParser.cs 125
public MGBuildParser(object optionsObject)
{
....
foreach(var pair in _optionalOptions)
{
var fi = GetAttribute<CommandLineParameterAttribute>(pair.Value);
if(!string.IsNullOrEmpty(fi.Flag))
_flags.Add(fi.Flag, fi.Name);
}
}
Eto.Forms
V3146 Possible null dereference of 'ofTypeMethod'. The 'FirstOrDefault' can return default null value. Eto BindingExtensionsNonGeneric.cs 21
public static IBinding BindingOfType(....)
{
....
var ofTypeMethod = bindingType.GetRuntimeMethods()
.FirstOrDefault(....);
return (IBinding)ofTypeMethod.MakeGenericMethod(toType)
.Invoke(...);
}
Discord.NET
V3146 Possible null dereference. The 'FirstOrDefault' can return default null value. BuildOverrides.cs 254
public sealed class BuildOverrides
{
private static Assembly
_overrideDomain_Resolving(AssemblyLoadContext arg1,
AssemblyName arg2)
{
var v = _loadedOverrides
.FirstOrDefault(x =>
x.Value.Any(x =>
x.Assembly.FullName == arg1.Assemblies
.FirstOrDefault().FullName)); // <=
return GetDependencyAsync(v.Key.Id, $"{arg2}").GetAwaiter()
.GetResult();
}
}
OrchardCore
V3146 Possible null dereference. The '_factories.FirstOrDefault' can return default null value. AddToDeploymentPlanController.cs 77
public async Task<IActionResult> AddContentItem(int deploymentPlanId,
string returnUrl,
string contentItemId)
{
var step = (ContentItemDeploymentStep)_factories.FirstOrDefault(x =>
x.Name == nameof(ContentItemDeploymentStep)).Create();
....
}