GeezerVison is showing github.com — Throw UnsupportedOperationException from AdaptiveByteBuf.memoryAddres… · netty/netty@60d9baf
File tree
Expand file treeCollapse file tree
Open diff view settings
main/java/io/netty/buffer test/java/io/netty/buffer
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2279,13 +2279,16 @@ public boolean hasMemoryAddress() { |
2279 | 2279 | @Override |
2280 | 2280 | public long memoryAddress() { |
2281 | 2281 | ensureAccessible(); |
| 2282 | + if (!hasMemoryAddress) { |
| 2283 | + throw new UnsupportedOperationException(); |
| 2284 | + } |
2282 | 2285 | return _memoryAddress(); |
2283 | 2286 | } |
2284 | 2287 |
|
2285 | 2288 | @Override |
2286 | 2289 | long _memoryAddress() { |
2287 | 2290 | AbstractByteBuf root = rootParent; |
2288 | | - return root != null ? root._memoryAddress() + startIndex : 0L; |
| 2291 | + return root != null && hasMemoryAddress ? root._memoryAddress() + startIndex : 0L; |
2289 | 2292 | } |
2290 | 2293 |
|
2291 | 2294 | @Override |
|
Collapse file
Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
79 | 79 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
80 | 80 | import static org.junit.jupiter.api.Assertions.assertEquals; |
81 | 81 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 82 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
82 | 83 | import static org.junit.jupiter.api.Assertions.assertNotSame; |
83 | 84 | import static org.junit.jupiter.api.Assertions.assertSame; |
84 | 85 | import static org.junit.jupiter.api.Assertions.assertThrows; |
@@ -4561,6 +4562,21 @@ public void testMemoryAddressAfterRelease() { |
4561 | 4562 | } |
4562 | 4563 | } |
4563 | 4564 |
|
| 4565 | + @Test |
| 4566 | + public void testMemoryAddressUnsupportedWhenNoMemoryAddress() { |
| 4567 | + ByteBuf buf = newBuffer(16); |
| 4568 | + try { |
| 4569 | + if (buf.hasMemoryAddress()) { |
| 4570 | + // Must not throw, and must not be the "no address" marker. |
| 4571 | + assertNotEquals(0L, buf.memoryAddress()); |
| 4572 | + } else { |
| 4573 | + assertThrows(UnsupportedOperationException.class, () -> buf.memoryAddress()); |
| 4574 | + } |
| 4575 | + } finally { |
| 4576 | + buf.release(); |
| 4577 | + } |
| 4578 | + } |
| 4579 | + |
4564 | 4580 | @Test |
4565 | 4581 | public void testSliceAfterRelease() { |
4566 | 4582 | assertThrows(IllegalReferenceCountException.class, new Executable() { |
|