CodeQL documentation

Redundant null check due to previous dereference

ID: cpp/redundant-null-check-simple
Kind: path-problem
Security severity: 7.5
Severity: error
Precision: high
Tags:
   - reliability
   - correctness
   - security
   - external/cwe/cwe-476
Query suites:
   - cpp-code-scanning.qls
   - cpp-security-extended.qls
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule finds comparisons of a pointer to null that occur after a reference of that pointer. It’s likely either the check is not required and can be removed, or it should be moved to before the dereference so that a null pointer dereference does not occur.

Recommendation

The check should be moved to before the dereference, in a way that prevents a null pointer value from being dereferenced. If it’s clear that the pointer cannot be null, consider removing the check instead.

Example

int f(MyList *list) {
	list->append(1);

	// ...

	if (list != NULL)
	{
		list->append(2);
	}
}

References

  • © GitHub, Inc.
  • Terms
  • Privacy