You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by Daniel Shane <sh...@LEXUM.UMontreal.CA> on 2009/09/08 21:32:20 UTC

Lucene 2.9-RC2 : Bug in equals() of TermAttributeImpl

If you look at the code from equals(), I think it misses this check :

"AX".equals("A");

since it does not check if the termLength() are different.

Here is an example of a fix, it may not be optimal, but I think checking 
size as the first thing is better than checking size after looping.

public boolean equals(Object other) {
    if (other == this) {
      return true;
    }
   
    //Check for size also
    if (termLength() != ((TermAttributeImpl)other).termLength()) {
      return false;
    }

    if (other instanceof TermAttribute) {
      initTermBuffer();
      TermAttributeImpl o = ((TermAttributeImpl) other);
      o.initTermBuffer();
      
      for(int i=0;i<termLength;i++) {
        if (termBuffer[i] != o.termBuffer[i]) {
          return false;
        }
      }
      return true;
    }
    
    return false;
  }

RE: Lucene 2.9-RC2 : Bug in equals() of TermAttributeImpl

Posted by Uwe Schindler <uw...@thetaphi.de>.
I fixed it! Thank you very much!!!

 

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de

  _____  

From: Uwe Schindler [mailto:uwe@thetaphi.de] 
Sent: Tuesday, September 08, 2009 9:47 PM
To: java-dev@lucene.apache.org
Subject: RE: Lucene 2.9-RC2 : Bug in equals() of TermAttributeImpl

 

You are right, I will fix it.

 

The simpliest is to check if (termlength!=o.thermLength) before the
for-loop. The code is then equals to Token.java (I think this was a
copy'n'paste bug).

 

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de

  _____  

From: Daniel Shane [mailto:shaned@LEXUM.UMontreal.CA] 
Sent: Tuesday, September 08, 2009 9:32 PM
To: java-dev@lucene.apache.org
Subject: Lucene 2.9-RC2 : Bug in equals() of TermAttributeImpl

 

If you look at the code from equals(), I think it misses this check :

"AX".equals("A");

since it does not check if the termLength() are different.

Here is an example of a fix, it may not be optimal, but I think checking
size as the first thing is better than checking size after looping.

public boolean equals(Object other) {
    if (other == this) {
      return true;
    }
    
    //Check for size also
    if (termLength() != ((TermAttributeImpl)other).termLength()) {
      return false;
    }

    if (other instanceof TermAttribute) {
      initTermBuffer();
      TermAttributeImpl o = ((TermAttributeImpl) other);
      o.initTermBuffer();
      
      for(int i=0;i<termLength;i++) {
        if (termBuffer[i] != o.termBuffer[i]) {
          return false;
        }
      }
      return true;
    }
    
    return false;
  } 


RE: Lucene 2.9-RC2 : Bug in equals() of TermAttributeImpl

Posted by Uwe Schindler <uw...@thetaphi.de>.
You are right, I will fix it.

 

The simpliest is to check if (termlength!=o.thermLength) before the
for-loop. The code is then equals to Token.java (I think this was a
copy'n'paste bug).

 

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de

  _____  

From: Daniel Shane [mailto:shaned@LEXUM.UMontreal.CA] 
Sent: Tuesday, September 08, 2009 9:32 PM
To: java-dev@lucene.apache.org
Subject: Lucene 2.9-RC2 : Bug in equals() of TermAttributeImpl

 

If you look at the code from equals(), I think it misses this check :

"AX".equals("A");

since it does not check if the termLength() are different.

Here is an example of a fix, it may not be optimal, but I think checking
size as the first thing is better than checking size after looping.

public boolean equals(Object other) {
    if (other == this) {
      return true;
    }
    
    //Check for size also
    if (termLength() != ((TermAttributeImpl)other).termLength()) {
      return false;
    }

    if (other instanceof TermAttribute) {
      initTermBuffer();
      TermAttributeImpl o = ((TermAttributeImpl) other);
      o.initTermBuffer();
      
      for(int i=0;i<termLength;i++) {
        if (termBuffer[i] != o.termBuffer[i]) {
          return false;
        }
      }
      return true;
    }
    
    return false;
  }