runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Dereferencing before `null` checks

Open VasilievSerg opened this issue 4 years ago • 5 comments

I found several strange code fragments: in all these fragments, references are dereferenced before they are checked for null. I'm not sure that all these code fragments have errors, but they look suspicious.

DocumentSchemaValidator.cs (link to the sources)

XmlSchemaAttribute schemaAttribute = (_defaultAttributes[i] as XmlSchemaAttribute)!;
attrQName = schemaAttribute.QualifiedName; // <=
Debug.Assert(schemaAttribute != null);     // <=

XmlSerializationWriterILGen.cs (link to the sources)

LocalBuilder local = initValue.ILG.DeclareOrGetLocal(localType, variableName);  // <=
if (initValue != null)  // <=      
{
  initValue.Load(local.LocalType);
  initValue.ILG.Stloc(local);
}

ExpressionBinder.cs (link to the sources)

private static RuntimeBinderException BadOperatorTypesError(Expr pOperand1, Expr pOperand2)
{
  // This is a hack, but we need to store the operation somewhere... the first argument's as
  // good a place as any.
  string strOp = pOperand1.ErrorString; // <=

  Debug.Assert(pOperand1 != null); // <=
  Debug.Assert(pOperand1.Type != null);
  ....
}

PipeCompletionCallbacks.cs (link to the sources)

public void Execute()
{
    var count = _callbacks.Count; // <=
    if (count == 0)
    {
      return;
    }

    List<Exception>? exceptions = null;

    if (_callbacks != null) // <=
            ....
}

Infer.cs (link to the sources)

if (childURI.Length == 0) // <=
{
    xs = parentSchema;
    add = false;
}
else if (childURI != null && !_schemaSet!.Contains(childURI))  // <=

MgmtConfigurationRecord.cs (link to the sources)

internal ConfigurationSection FindImmediateParentSection(ConfigurationSection section)
{
    ConfigurationSection result = null;

    string configKey = section.SectionInformation.SectionName;
    SectionRecord sectionRecord = GetSectionRecord(configKey, false);
    if (sectionRecord.HasLocationInputs)
    {
        SectionInput input = sectionRecord.LastLocationInput; // <=
        Debug.Assert(input.HasResult, "input.HasResult");  // <=
        result = (ConfigurationSection)input.Result;
    }
    else
    {
        if (sectionRecord.HasIndirectLocationInputs)
        {
            Debug.Assert(IsLocationConfig, "Indirect location inputs exist only in location config record");
            SectionInput input = sectionRecord.LastIndirectLocationInput;
            Debug.Assert(input != null);
            Debug.Assert(input.HasResult, "input.HasResult");
            result = (ConfigurationSection)input.Result;
         }
         ....
    ....
}

VasilievSerg avatar Nov 21 '22 13:11 VasilievSerg

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost avatar Nov 21 '22 13:11 ghost

Tagging subscribers to this area: @dotnet/area-system-xml See info in area-owners.md if you want to be subscribed.

Issue Details

I found several strange code fragments: in all these fragments, references are dereferenced before they are checked for null. I'm not sure that all these code fragments have errors, but they look suspicious.

DocumentSchemaValidator.cs (link to the sources)

XmlSchemaAttribute schemaAttribute = (_defaultAttributes[i] as XmlSchemaAttribute)!;
attrQName = schemaAttribute.QualifiedName; // <=
Debug.Assert(schemaAttribute != null);     // <=

XmlSerializationWriterILGen.cs (link to the sources)

LocalBuilder local = initValue.ILG.DeclareOrGetLocal(localType, variableName);  // <=
if (initValue != null)  // <=      
{
  initValue.Load(local.LocalType);
  initValue.ILG.Stloc(local);
}

ExpressionBinder.cs (link to the sources)

private static RuntimeBinderException BadOperatorTypesError(Expr pOperand1, Expr pOperand2)
{
  // This is a hack, but we need to store the operation somewhere... the first argument's as
  // good a place as any.
  string strOp = pOperand1.ErrorString; // <=

  Debug.Assert(pOperand1 != null); // <=
  Debug.Assert(pOperand1.Type != null);
  ....
}

PipeCompletionCallbacks.cs (link to the sources)

public void Execute()
{
    var count = _callbacks.Count; // <=
    if (count == 0)
    {
      return;
    }

    List<Exception>? exceptions = null;

    if (_callbacks != null) // <=
            ....
}

Infer.cs (link to the sources)

if (childURI.Length == 0) // <=
{
    xs = parentSchema;
    add = false;
}
else if (childURI != null && !_schemaSet!.Contains(childURI))  // <=

MgmtConfigurationRecord.cs (link to the sources)

internal ConfigurationSection FindImmediateParentSection(ConfigurationSection section)
{
    ConfigurationSection result = null;

    string configKey = section.SectionInformation.SectionName;
    SectionRecord sectionRecord = GetSectionRecord(configKey, false);
    if (sectionRecord.HasLocationInputs)
    {
        SectionInput input = sectionRecord.LastLocationInput; // <=
        Debug.Assert(input.HasResult, "input.HasResult");  // <=
        result = (ConfigurationSection)input.Result;
    }
    else
    {
        if (sectionRecord.HasIndirectLocationInputs)
        {
            Debug.Assert(IsLocationConfig, "Indirect location inputs exist only in location config record");
            SectionInput input = sectionRecord.LastIndirectLocationInput;
            Debug.Assert(input != null);
            Debug.Assert(input.HasResult, "input.HasResult");
            result = (ConfigurationSection)input.Result;
         }
         ....
    ....
}
Author: VasilievSerg
Assignees: -
Labels:

area-System.Xml, untriaged

Milestone: -

ghost avatar Nov 21 '22 15:11 ghost

MgmtConfigurationRecord.cs (link to the sources)

What's the issue in this one? I'm not seeing it.

stephentoub avatar Nov 22 '22 20:11 stephentoub

What's the issue in this one? I'm not seeing it.

I should have given a more detailed explanation. I'm sorry.

It looks like the both LastLocationInput and LastIndirectLocationInput properties may return null.

internal SectionInput LastLocationInput => HasLocationInputs ? LocationInputs[LocationInputs.Count - 1] : null;

Link to the sources

internal SectionInput LastIndirectLocationInput
  => HasIndirectLocationInputs ? IndirectLocationInputs[IndirectLocationInputs.Count - 1] : null;

Link to the sources

The value of LastIndirectLocationInput is assigned to the input variable. After the assignment, input is checked for null. After this the HasResult property is also checked:

SectionInput input = sectionRecord.LastIndirectLocationInput;
Debug.Assert(input != null);
Debug.Assert(input.HasResult, "input.HasResult");

The value of the LastLocalInput is assigned to the input variable. input is also checked for the result (HasResult), but is not checked for the null value:

SectionInput input = sectionRecord.LastLocationInput;
// Debug.Assert(input != null) ?
Debug.Assert(input.HasResult, "input.HasResult");

It looks a little bit suspicious.

VasilievSerg avatar Nov 23 '22 06:11 VasilievSerg

I see, thanks, you're pointing out the lack of consistency around the assert. We could add one to the top block but I don't think it's worth the churn.

stephentoub avatar Nov 23 '22 13:11 stephentoub