C# 强化:空解引用
当使用Fortify扫描C#项目时,发现之一是: Helper.cs中的ValidateSchema()方法可以取消对第95行的null指针的引用,从而引发null异常 当我在第93行显式检查C# 强化:空解引用,c#,static-analysis,fortify,C#,Static Analysis,Fortify,当使用Fortify扫描C#项目时,发现之一是: Helper.cs中的ValidateSchema()方法可以取消对第95行的null指针的引用,从而引发null异常 当我在第93行显式检查xml是否为null时,我无法理解这是如何发生的。根据需要,Validate()的第一个参数是可选的 以下是对强化的分析: Analysis Trace Helper.cs:93 - Branch taken: (xml != null) Helper.cs:95 - null : is declared
xml
是否为null时,我无法理解这是如何发生的。根据需要,Validate()
的第一个参数是可选的
以下是对强化的分析:
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: }
似乎是Fortify中的一个bug-可能将
null
传递给Validate
会混淆它?我不认为该参数是这里出现问题的原因,因为跟踪中的最后一行表明xml
为null。