From 2a0680c2bae72d76d0072983cd9eeed5ee494e2e Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Fri, 14 Dec 2018 12:07:51 +0100 Subject: [PATCH 1/2] Fix StringSequence.equals() for different lengths See gh-15465 --- .../springframework/boot/loader/jar/StringSequence.java | 2 +- .../boot/loader/jar/StringSequenceTests.java | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java index 896c45561b12..1e3154327512 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java @@ -126,7 +126,7 @@ public boolean equals(Object obj) { } return true; } - return true; + return false; } @Override diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/StringSequenceTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/StringSequenceTests.java index d9d2d48ece44..fc111c90ba8d 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/StringSequenceTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/StringSequenceTests.java @@ -167,6 +167,13 @@ public void equalsWhenSameContentShouldMatch() { assertThat(a).isEqualTo(b).isNotEqualTo(c); } + @Test + public void notEqualsWhenSequencesOfDifferentLength() { + StringSequence a = new StringSequence("abcd"); + StringSequence b = new StringSequence("ef"); + assertThat(a).isNotEqualTo(b); + } + @Test public void startsWithWhenExactMatch() { assertThat(new StringSequence("abc").startsWith("abc")).isTrue(); From c0b07a942966acf7935cde2ed488cf010875ada6 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 17 Dec 2018 18:24:08 +0100 Subject: [PATCH 2/2] Polish "Fix StringSequence.equals() for different lengths" Closes gh-15465 --- .../boot/loader/jar/StringSequence.java | 20 +++++++++---------- .../boot/loader/jar/StringSequenceTests.java | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java index 1e3154327512..9d5b05fe25ff 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java @@ -111,22 +111,22 @@ public boolean equals(Object obj) { if (this == obj) { return true; } - if (obj == null || !CharSequence.class.isInstance(obj)) { + if (!(obj instanceof CharSequence)) { return false; } CharSequence other = (CharSequence) obj; int n = length(); - if (n == other.length()) { - int i = 0; - while (n-- != 0) { - if (charAt(i) != other.charAt(i)) { - return false; - } - i++; + if (n != other.length()) { + return false; + } + int i = 0; + while (n-- != 0) { + if (charAt(i) != other.charAt(i)) { + return false; } - return true; + i++; } - return false; + return true; } @Override diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/StringSequenceTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/StringSequenceTests.java index fc111c90ba8d..b0097138867e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/StringSequenceTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/StringSequenceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.