JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
Java


JAVA.IDEF.CTOEQ : compareTo/equals mismatch (Java)

Summary

compareTo() is defined is defined inconsistently from equals().

By convention the compareTo should return 0 if two instances are equal, and it should return 1 or -1 only if the instances are different in order to preserve the total ordering.

A class C that implements the raw interface java.lang.Comparable must implement the compareTo(Object) method. If C is made to implement the non-raw interface java.lang.Comparable<C>, then it must implement the compareTo(C) method instead.

Moreover, it is good practice to make compareTo() consistent with equals(): if the comparison of two objects yields 0, then they should be equal. The validity of this implication is in general undecidable. There are, however, frequent situations when, typically, this implication does not hold. An example is when equals() is inherited from java.lang.Object.

This checker verifies that compareTo(Object) is defined for classes implementing the raw java.lang.Comparable interface, instead of the (possibly more logical) method compareTo(C). Moreover, it verifies the consistency of compareTo() wrt equals().

Inconsistent definitions of compareTo()/equals() induce unexpected behaviors when objects are put inside most SortedSet classes of the standard Java library.

Properties

Class Name compareTo/equals mismatch (Java)
Significance reliability
Mnemonic JAVA.IDEF.CTOEQ
Categories
CWE CWE:697 Incorrect Comparison
CERT-Java CERT-Java:MET08-J Preserve the equality contract when overriding the equals() method
Availability Available for Java and Kotlin.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="compareTo/equals mismatch (Java)"

Example

// CompareToVsEquals9.java
public final class CompareToVsEquals9 implements Comparable<CompareToVsEquals9> {
  private int f;
        
  public CompareToVsEquals9(int f) {
    this.f = f;
  }

  @Override
  public int compareTo(CompareToVsEquals9 o) {
    if (this == o || this.equals(o)) // "compareTo/equals mismatch (Java)" warning issued here
      return 1;
    else
      return -1;
  }

  @Override
  public boolean equals(Object other) {
    return other instanceof CompareToVsEquals9 && ((CompareToVsEquals9) other).f == f;
  }

  @Override
  public String toString() {
    return String.valueOf(f);
  }
}    
// Mango.java
public class Mango implements Comparable<Mango> {
  public int compareTo(Mango o) { // "compareTo/equals mismatch (Java)" warning issued here
      if (this == o || this.equals(o))
          return 1;
      else
          return -1;
  }
        
  //...
}

Resolution

RespectJava convention, the compareTo should return 0 if two instances are equal, and it should return 1 or -1 only if the instances are different in order to preserve the total ordering.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.