Skip to content

Commit 4b607bd

Browse files
committed
Polish 'Protected against JsonValueWriter stack overflow'
See gh-44627
1 parent b5e0eed commit 4b607bd

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
*/
4848
class JsonValueWriter {
4949

50-
private static final int DEFAULT_MAX_NESTING_DEPTH = 1000;
50+
private static final int DEFAULT_MAX_NESTING_DEPTH = 500;
5151

5252
private final Appendable out;
5353

@@ -160,7 +160,10 @@ private <V> boolean canWriteAsArray(Iterable<?> iterable) {
160160
*/
161161
void start(Series series) {
162162
if (series != null) {
163-
validateNestingDepth();
163+
int nestingDepth = this.activeSeries.size();
164+
Assert.state(nestingDepth <= this.maxNestingDepth,
165+
() -> "JSON nesting depth (%s) exceeds maximum depth of %s (current path: %s)"
166+
.formatted(nestingDepth, this.maxNestingDepth, this.path));
164167
this.activeSeries.push(new ActiveSeries(series));
165168
append(series.openChar);
166169
}
@@ -288,13 +291,6 @@ private void writeString(Object value) {
288291
}
289292
}
290293

291-
private void validateNestingDepth() {
292-
if (this.activeSeries.size() > this.maxNestingDepth) {
293-
throw new IllegalStateException("JSON nesting depth (%s) exceeds maximum depth of %s (current path: %s)"
294-
.formatted(this.activeSeries.size(), this.maxNestingDepth, this.path));
295-
}
296-
}
297-
298294
private void append(String value) {
299295
try {
300296
this.out.append(value);

0 commit comments

Comments
 (0)