NPD.CHECK.CALL.MUSTPreviously checked null pointer is dereferenced through a function callAn attempt to access data using a null pointer causes a runtime error. When a program dereferences a pointer that is expected to be valid but turns out to be null, a null pointer dereference occurs. Null-pointer dereference defects often occur due to ineffective error handling or race conditions, and typically cause abnormal program termination. Before a pointer is dereferenced in C/C++ code, it must be checked to confirm that it is not equal to null. The NPD checkers look for instances in which a null or possibly null pointer is dereferenced. The NPD.CHECK.CALL.MUST checker flags situations in which a pointer that's been checked for a null value is subsequently passed to a function that dereferences it without checking it for null. Vulnerability and riskNull-pointer dereferences usually result in the failure of the process. These issues typically occur due to ineffective exception handling. Mitigation and preventionTo avoid this vulnerability:
Vulnerable code example1 void reassign(int *argument, int *p) { 2 if (goodEnough(argument)) return; 3 *argument = *p; 4 } 5 6 void npd_check_call_must(int *argument) { 7 int *p = getValue(); 8 if (p != 0) { 9 *p = 1; 10 } 11 reassign(argument, p); 12 } Although *p is checked for null at line 8, it's then passed to function reassign, in which it is dereferenced without being checked for null. This type of vulnerability can produce unexpected and unintended results. Fixed code example1 void reassign(int *argument, int *p) { 2 if (goodEnough(argument)) return; 3 *argument = *p; 4 } 5 6 void npd_check_call_must(int *argument) { 7 int *p = getValue(); 8 if (p != 0) { 9 *p = 1; 10 } 11 if (p != 0) reassign(argument, p); 12 } In the fixed version of the code, a second check for null has been put in line 11. Related checkersExtensionThis checker can be extended through the Klocwork knowledge base. See Tuning C/C++ analysis for more information. |