Fortify:空取消引用

问题描述

使用 Fortify 扫描 C# 项目时,发现之一是:

Helper.cs 中的 ValidateSchema() 方法可以在第 95 行取消引用空指针,从而引发 NullException。

当我在第 93 行明确检查 xml 是否为空时,我无法理解这是如何发生的。根据 https://docs.microsoft.com/de-de/dotnet/api/system.xml.xmldocument.validate?view=net-5.0Validate() 的第一个参数是可选的。

以下是 Fortify 的分析:

Analysis Trace
Helper.cs:93 - Branch taken: (xml != null)
Helper.cs:95 - null : is declared and/or assigned to null value
Helper.cs:95 - null.Validate() : is not checked for null value before being dereferenced

代码如下:

 84:        public static bool ValidateSchema(string xmlPath,string xsdpath)
 85:        {
 86:            XmlDocument xml = new XmlDocument();
 87:            xml.Load(xmlPath);
 88:
 89:            xml.Schemas.Add(null,xsdpath);
 90:
 91:            try
 92:            {
 93:                if (xml != null)
 94:                {
 95:                    xml.Validate(null);
 96:                }
 97:                else
 98:                {
 99:                    return false;
100:                }
101:            }
102:            catch
103:            {
104:                return false;
105:            }
106:
107:            return true;
108:        }
109:    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)