Browse Source

Release DataBuffer in AbstractCharSequenceDecoder

if String creation fails

See gh-35625

Signed-off-by: Marius Lichtblau <marius@lichtblau.io>
pull/35672/head
Marius Lichtblau 2 months ago committed by rstoyanchev
parent
commit
ba2bb08589
  1. 20
      spring-core/src/main/java/org/springframework/core/codec/AbstractCharSequenceDecoder.java
  2. 4
      spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferLimitException.java
  3. 7
      spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java

20
spring-core/src/main/java/org/springframework/core/codec/AbstractCharSequenceDecoder.java

@ -175,14 +175,18 @@ public abstract class AbstractCharSequenceDecoder<T extends CharSequence> extend
public final T decode(DataBuffer dataBuffer, ResolvableType elementType, public final T decode(DataBuffer dataBuffer, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
Charset charset = getCharset(mimeType); try {
T value = decodeInternal(dataBuffer, charset); Charset charset = getCharset(mimeType);
DataBufferUtils.release(dataBuffer); T value = decodeInternal(dataBuffer, charset);
LogFormatUtils.traceDebug(logger, traceOn -> { LogFormatUtils.traceDebug(logger, traceOn -> {
String formatted = LogFormatUtils.formatValue(value, !traceOn); String formatted = LogFormatUtils.formatValue(value, !traceOn);
return Hints.getLogPrefix(hints) + "Decoded " + formatted; return Hints.getLogPrefix(hints) + "Decoded " + formatted;
}); });
return value; return value;
}
finally {
DataBufferUtils.release(dataBuffer);
}
} }
private Charset getCharset(@Nullable MimeType mimeType) { private Charset getCharset(@Nullable MimeType mimeType) {

4
spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferLimitException.java

@ -35,4 +35,8 @@ public class DataBufferLimitException extends IllegalStateException {
super(message); super(message);
} }
public DataBufferLimitException(String message, Throwable cause) {
super(message, cause);
}
} }

7
spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java

@ -379,7 +379,12 @@ public class NettyDataBuffer implements PooledDataBuffer {
@Override @Override
public String toString() { public String toString() {
return this.byteBuf.toString(); try {
return this.byteBuf.toString();
}
catch (OutOfMemoryError ex) {
throw new DataBufferLimitException("Failed to convert data buffer to string: " + ex.getMessage(), ex);
}
} }

Loading…
Cancel
Save