Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging a Path object using structured logging throws StackOverflowError #44507

Open
wants to merge 1 commit into
base: 3.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
Expand All @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
Expand Down Expand Up @@ -114,6 +115,10 @@ else if (value instanceof WritableJson writableJson) {
throw new UncheckedIOException(ex);
}
}
// https://github.com/spring-projects/spring-boot/issues/44502
else if (value instanceof Path p) {
writeString(p.toString());
}
else if (value instanceof Iterable<?> iterable) {
writeArray(iterable::forEach);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
Expand All @@ -16,6 +16,7 @@

package org.springframework.boot.json;

import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -240,6 +241,18 @@ void endWhenNotStartedThrowsException() {
.isThrownBy(() -> valueWriter.end(Series.ARRAY)));
}

// https://github.com/spring-projects/spring-boot/issues/44502
@Test
void writeJavaNioPathWhenSingleElementShouldNotCauseStackOverflowError() {
assertThat(doWrite((valueWriter) -> valueWriter.write(Path.of("overflow")))).isEqualTo(quoted("overflow"));
}

@Test
void writeJavaNioPathShouldNotCauseStackOverflowError() {
assertThat(doWrite((valueWriter) -> valueWriter.write(Path.of("stack/overflow/error"))))
.isEqualTo(quoted("stack\\/overflow\\/error"));
}

private <V> String write(V value) {
return doWrite((valueWriter) -> valueWriter.write(value));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
Expand All @@ -16,9 +16,12 @@

package smoketest.structuredlogging;

import java.nio.file.Path;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.LoggerFactory;

import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.LoggingSystemProperty;
Expand All @@ -43,6 +46,18 @@ void reset() {
}
}

// https://github.com/spring-projects/spring-boot/issues/44502
@Test
void javaNioPathShouldNotCauseStackOverflowError(CapturedOutput output) {
SampleStructuredLoggingApplication.main(new String[0]);
LoggerFactory.getLogger(SampleStructuredLoggingApplication.class)
.atInfo()
.addKeyValue("directory", Path.of("stack/overflow/error"))
.log("java.nio.file.Path works as expected");
assertThat(output).contains("java.nio.file.Path works as expected").contains("""
"directory":"stack\\/overflow\\/error""");
}

@Test
void shouldNotLogBanner(CapturedOutput output) {
SampleStructuredLoggingApplication.main(new String[0]);
Expand Down
1 change: 1 addition & 0 deletions src/checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<suppress files="LogbackLoggingSystemTests\.java" checks="IllegalImport" />
<suppress files="LogbackLoggingSystemParallelInitializationTests\.java" checks="IllegalImport" />
<suppress files="LogbackConfigurationAotContributionTests\.java" checks="IllegalImport" />
<suppress files="SampleStructuredLoggingApplicationTests\.java" checks="IllegalImport" message="LoggerFactory" />
<suppress files="MetricsAutoConfigurationMeterRegistryPostProcessorIntegrationTests\.java" checks="IllegalImport" message="LoggerFactory"/>
<suppress files="SpringApplicationTests\.java" checks="FinalClass" />
<suppress files=".+Configuration\.java" checks="HideUtilityClassConstructor" />
Expand Down
Loading