diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedPropertiesLoader.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedPropertiesLoader.java index 110754fcf77..a4b8bf9e45c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedPropertiesLoader.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedPropertiesLoader.java @@ -224,17 +224,17 @@ class OriginTrackedPropertiesLoader { this.character = 0; for (int i = 0; i < 4; i++) { int digit = this.reader.read(); - if (digit > -'0' && digit <= '9') { + if (digit >= '0' && digit <= '9') { this.character = (this.character << 4) + digit - '0'; } - else if (digit > -'a' && digit <= 'f') { + else if (digit >= 'a' && digit <= 'f') { this.character = (this.character << 4) + digit - 'a' + 10; } - else if (digit > -'A' && digit <= 'F') { + else if (digit >= 'A' && digit <= 'F') { this.character = (this.character << 4) + digit - 'A' + 10; } else { - throw new IllegalArgumentException("Malformed \\uxxxx encoding."); + throw new IllegalStateException("Malformed \\uxxxx encoding."); } } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/OriginTrackedPropertiesLoaderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/OriginTrackedPropertiesLoaderTests.java index 00127bc0792..2347c3df697 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/OriginTrackedPropertiesLoaderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/OriginTrackedPropertiesLoaderTests.java @@ -20,7 +20,9 @@ import java.util.Map; import java.util.Properties; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.springframework.boot.origin.OriginTrackedValue; import org.springframework.boot.origin.TextResourceOrigin; @@ -37,6 +39,9 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class OriginTrackedPropertiesLoaderTests { + @Rule + public ExpectedException thrown = ExpectedException.none(); + private ClassPathResource resource; private Map properties; @@ -85,6 +90,15 @@ public class OriginTrackedPropertiesLoaderTests { assertThat(getLocation(value)).isEqualTo("12:14"); } + @Test + public void getMalformedUnicodeProperty() throws Exception { + // gh-2716 + this.thrown.expect(IllegalStateException.class); + this.thrown.expectMessage("Malformed \\uxxxx encoding"); + new OriginTrackedPropertiesLoader(new ClassPathResource( + "test-properties-malformed-unicode.properties", getClass())).load(); + } + @Test public void getEscapedProperty() { OriginTrackedValue value = this.properties.get("test=property"); diff --git a/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/env/test-properties-malformed-unicode.properties b/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/env/test-properties-malformed-unicode.properties new file mode 100644 index 00000000000..8a9e5922993 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/org/springframework/boot/env/test-properties-malformed-unicode.properties @@ -0,0 +1 @@ +test-malformed-unicode=properties\u(026test