程序會間接引用一個空指針,因為它不對有可能返回 Null 的函數返回值進行檢查。
幾乎每一個對軟件系統的嚴重攻擊都是從違反程序員的假設開始的。攻擊后,程序員的假設看起來既脆弱又拙劣,但攻擊前,許多程序員會在午休時間為自己的種種假設做很好的辯護。
在代碼中很容易發現的兩個可疑的假設是:一是這個函數調用不可能出錯;二是即使出錯了,也不會對系統造成什么重要影響。當程序員忽略函數返回值時,就暗示著自己是基于上述任一假設來執行操作。
例 1:以下代碼在調用成員函數 compareTo() 之前,不會檢查 getParameter() 返回的字符串是否為 Null,因此可能會造成 null dereference。
String itemName = request.getParameter(ITEM_NAME);
if (itemName.compareTo(IMPORTANT_ITEM)) {
...
}
...
System.clearProperty("os.name");
...
String os = System.getProperty("os.name");
if (os.equalsIgnoreCase("Windows 95") )
System.out.println("Not supported");
[1] Standards Mapping - OWASP Top 10 2004 - (OWASP 2004) A9 Application Denial of Service
[2] Standards Mapping - Security Technical Implementation Guide Version 3 - (STIG 3) APP3120 CAT II, APP6080 CAT II
[3] Standards Mapping - Security Technical Implementation Guide Version 3.4 - (STIG 3.4) APP3120 CAT II, APP6080 CAT II
[4] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 253, CWE ID 690
[5] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 - (PCI 1.1) Requirement 6.5.9