Skip to content

Commit ba2bb08

Browse files
mlichtblaurstoyanchev
authored andcommitted
Release DataBuffer in AbstractCharSequenceDecoder
if String creation fails See gh-35625 Signed-off-by: Marius Lichtblau <marius@lichtblau.io>
1 parent 141df52 commit ba2bb08

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,18 @@ private Collection<DataBuffer> processDataBuffer(DataBuffer buffer, DataBufferUt
175175
public final T decode(DataBuffer dataBuffer, ResolvableType elementType,
176176
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
177177

178-
Charset charset = getCharset(mimeType);
179-
T value = decodeInternal(dataBuffer, charset);
180-
DataBufferUtils.release(dataBuffer);
181-
LogFormatUtils.traceDebug(logger, traceOn -> {
182-
String formatted = LogFormatUtils.formatValue(value, !traceOn);
183-
return Hints.getLogPrefix(hints) + "Decoded " + formatted;
184-
});
185-
return value;
178+
try {
179+
Charset charset = getCharset(mimeType);
180+
T value = decodeInternal(dataBuffer, charset);
181+
LogFormatUtils.traceDebug(logger, traceOn -> {
182+
String formatted = LogFormatUtils.formatValue(value, !traceOn);
183+
return Hints.getLogPrefix(hints) + "Decoded " + formatted;
184+
});
185+
return value;
186+
}
187+
finally {
188+
DataBufferUtils.release(dataBuffer);
189+
}
186190
}
187191

188192
private Charset getCharset(@Nullable MimeType mimeType) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,8 @@ public DataBufferLimitException(String message) {
3535
super(message);
3636
}
3737

38+
public DataBufferLimitException(String message, Throwable cause) {
39+
super(message, cause);
40+
}
41+
3842
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,12 @@ public int hashCode() {
379379

380380
@Override
381381
public String toString() {
382-
return this.byteBuf.toString();
382+
try {
383+
return this.byteBuf.toString();
384+
}
385+
catch (OutOfMemoryError ex) {
386+
throw new DataBufferLimitException("Failed to convert data buffer to string: " + ex.getMessage(), ex);
387+
}
383388
}
384389

385390

0 commit comments

Comments
 (0)