2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2021-06-08 14:23:48 +02:00
|
|
|
Failing to null-test the argument to an ``++equals++`` method could result in a null pointer dereference, leading to runtime failures.
|
|
|
|
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2021-06-08 14:23:48 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,text]
|
2021-06-08 14:23:48 +02:00
|
|
|
----
|
|
|
|
public bool Equals (object obj) { // Noncompliant
|
|
|
|
return getValue() == obj.getValue() ;
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2021-06-08 14:23:48 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,text]
|
2021-06-08 14:23:48 +02:00
|
|
|
----
|
|
|
|
public bool Equals (object obj) {
|
|
|
|
if (obj == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return getValue() == obj.getValue() ;
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
== Resources
|
2021-06-08 14:23:48 +02:00
|
|
|
|
2024-01-15 17:15:56 +01:00
|
|
|
* CWE - https://cwe.mitre.org/data/definitions/476[CWE-476 - NULL Pointer Dereference]
|
2021-06-08 14:23:48 +02:00
|
|
|
|