Skip to content

Commit

Permalink
Fix properties unicode value decoding
Browse files Browse the repository at this point in the history
Fix a range error when checking for unicode hex chars.

Fixes spring-projectsgh-12716
  • Loading branch information
philwebb committed Apr 2, 2018
1 parent 6831628 commit a657a28
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,17 @@ private void readUnicode() throws IOException {
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.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
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;
Expand All @@ -37,6 +39,9 @@
*/
public class OriginTrackedPropertiesLoaderTests {

@Rule
public ExpectedException thrown = ExpectedException.none();

private ClassPathResource resource;

private Map<String, OriginTrackedValue> properties;
Expand Down Expand Up @@ -85,6 +90,15 @@ public void getUnicodeProperty() {
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");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test-malformed-unicode=properties\u(026test

0 comments on commit a657a28

Please sign in to comment.