Skip to content

Commit

Permalink
fix: stringutils stackoverflowerror. (apache#2427)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbxyyx authored Mar 20, 2020
1 parent 7b796c3 commit abd96c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion common/src/main/java/io/seata/common/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ public static String toString(Object obj) {
sb.append("=");
try {
Object f = field.get(obj);
sb.append(toString(f));
if (f.getClass() == obj.getClass()) {
sb.append(f.toString());
} else {
sb.append(toString(f));
}
} catch (Exception e) {
}
sb.append(";");
Expand Down
27 changes: 27 additions & 0 deletions common/src/test/java/io/seata/common/util/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,31 @@ void testEqualsIgnoreCase() {
Assertions.assertFalse(StringUtils.equalsIgnoreCase("", null));
Assertions.assertFalse(StringUtils.equalsIgnoreCase(null, ""));
}

@Test
void testCycleDependency() {
CycleDependency A = CycleDependency.A;
try {
StringUtils.toString(A);
} catch (StackOverflowError e) {
Assertions.fail("stack overflow error");
}
}

static class CycleDependency {
public static final CycleDependency A = new CycleDependency("a");
public static final CycleDependency B = new CycleDependency("b");

private String s;
private CycleDependency(String s) {
this.s = s;
}

@Override
public String toString() {
return "{" +
"s='" + s + '\'' +
'}';
}
}
}

0 comments on commit abd96c1

Please sign in to comment.