Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String index out of bounds: 3 #226

Closed
runeflobakk opened this issue Dec 25, 2018 · 3 comments
Closed

String index out of bounds: 3 #226

runeflobakk opened this issue Dec 25, 2018 · 3 comments

Comments

@runeflobakk
Copy link

I think the latest version of equalsverifier introduced a bug in AnnotationCacheBuilder, more specifically line 106. The code assumes that methods which starts with "get" will always have more characters, and will throw a StringIndexOutOfBoundsException on methods simply named get(..)

What steps will reproduce the problem?

Basic EqualsVerifier-test on any class with a method named get.

What is the code that triggers this problem?

Given this class:

public final class MyValue {

    private final long value;

    public MyValue(long value) {
        this.value = value;
    }

    public long get() {
        return value;
    }

    @Override
    public boolean equals(Object o) {
        return o instanceof MyValue && ((MyValue) o).value == this.value;
    }

    @Override
    public int hashCode() {
        return Long.hashCode(value);
    }
}

And this JUnit-test:

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Test;

public class MyValueTest {
    @Test
    public void correctEqualsAndHashCode() {
        EqualsVerifier.forClass(MyValue.class).verify();
    }
}

The test is green on version 3.0.3, but throws the exception below on version 3.1.

What error message or stack trace does EqualsVerifier give?

java.lang.AssertionError: EqualsVerifier found a problem in class MyValue.
-> String index out of range: 3

For more information, go to: http://www.jqno.nl/equalsverifier/errormessages
	at nl.jqno.equalsverifier.EqualsVerifierApi.verify(EqualsVerifierApi.java:308)
	at MyValueTest.correctEqualsAndHashCode(MyValueTest.java:8)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 3
	at java.lang.String.charAt(String.java:658)
	at nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCacheBuilder.lambda$visitFields$13(AnnotationCacheBuilder.java:106)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCacheBuilder.visitFields(AnnotationCacheBuilder.java:104)
	at nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCacheBuilder.visitType(AnnotationCacheBuilder.java:47)
	at nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCacheBuilder.build(AnnotationCacheBuilder.java:35)
	at nl.jqno.equalsverifier.internal.util.Configuration.buildAnnotationCache(Configuration.java:79)
	at nl.jqno.equalsverifier.internal.util.Configuration.build(Configuration.java:67)
	at nl.jqno.equalsverifier.EqualsVerifierApi.buildConfig(EqualsVerifierApi.java:354)
	at nl.jqno.equalsverifier.EqualsVerifierApi.performVerification(EqualsVerifierApi.java:346)
	at nl.jqno.equalsverifier.EqualsVerifierApi.verify(EqualsVerifierApi.java:302)
	... 24 more

What did you expect?

Green test.

Which version of EqualsVerifier are you using?

Version 3.1.

@jqno
Copy link
Owner

jqno commented Dec 26, 2018

Thanks for reporting this, it's indeed a bug introduced in version 3.1 at the place you mention.
I've just released version 3.1.1 with a fix!

@jqno jqno closed this as completed Dec 26, 2018
@runeflobakk
Copy link
Author

Wow, that was quick! Thank you!

@jqno
Copy link
Owner

jqno commented Dec 26, 2018

You're welcome :)

akhalikov pushed a commit to akhalikov/equalsverifier that referenced this issue Nov 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants