Skip to content

Commit d3d8f1a

Browse files
committed
LimitedDataBufferList adds or raises error
Closes gh-26060
1 parent a637f6a commit d3d8f1a

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,11 +54,8 @@ public LimitedDataBufferList(int maxByteCount) {
5454

5555
@Override
5656
public boolean add(DataBuffer buffer) {
57-
boolean result = super.add(buffer);
58-
if (result) {
59-
updateCount(buffer.readableByteCount());
60-
}
61-
return result;
57+
updateCount(buffer.readableByteCount());
58+
return super.add(buffer);
6259
}
6360

6461
@Override

spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferUtilsTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import java.util.concurrent.CountDownLatch;
3636

3737
import io.netty.buffer.ByteBuf;
38+
import io.netty.buffer.PooledByteBufAllocator;
39+
import org.junit.jupiter.api.Test;
3840
import org.mockito.stubbing.Answer;
3941
import org.reactivestreams.Subscription;
4042
import reactor.core.publisher.BaseSubscriber;
@@ -834,6 +836,22 @@ void joinWithLimit(String displayName, DataBufferFactory bufferFactory) {
834836
.verifyError(DataBufferLimitException.class);
835837
}
836838

839+
@Test // gh-26060
840+
void joinWithLimitDoesNotOverRelease() {
841+
NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT);
842+
byte[] bytes = "foo-bar-baz".getBytes(StandardCharsets.UTF_8);
843+
844+
NettyDataBuffer buffer = bufferFactory.allocateBuffer(bytes.length);
845+
buffer.getNativeBuffer().retain(); // should be at 2 now
846+
buffer.write(bytes);
847+
848+
Mono<DataBuffer> result = DataBufferUtils.join(Flux.just(buffer), 8);
849+
850+
StepVerifier.create(result).verifyError(DataBufferLimitException.class);
851+
assertThat(buffer.getNativeBuffer().refCnt()).isEqualTo(1);
852+
buffer.release();
853+
}
854+
837855
@ParameterizedDataBufferAllocatingTest
838856
void joinErrors(String displayName, DataBufferFactory bufferFactory) {
839857
super.bufferFactory = bufferFactory;

spring-core/src/test/java/org/springframework/core/io/buffer/LimitedDataBufferListTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
import java.nio.charset.StandardCharsets;
1919

20-
import org.assertj.core.api.Assertions;
2120
import org.junit.jupiter.api.Test;
2221

22+
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
24+
2325
/**
2426
* Unit tests for {@link LimitedDataBufferList}.
2527
* @author Rossen Stoyanchev
@@ -32,8 +34,10 @@ public class LimitedDataBufferListTests {
3234

3335
@Test
3436
void limitEnforced() {
35-
Assertions.assertThatThrownBy(() -> new LimitedDataBufferList(5).add(toDataBuffer("123456")))
36-
.isInstanceOf(DataBufferLimitException.class);
37+
LimitedDataBufferList list = new LimitedDataBufferList(5);
38+
39+
assertThatThrownBy(() -> list.add(toDataBuffer("123456"))).isInstanceOf(DataBufferLimitException.class);
40+
assertThat(list).isEmpty();
3741
}
3842

3943
@Test

0 commit comments

Comments
 (0)