GeezerVison is showing github.com — Always correctly calculate the memory address of the ByteBuf even if … · netty/netty@79faea3
File tree
Expand file treeCollapse file tree
Open diff view settings
handler/src/main/java/io/netty/handler/ssl transport-native-unix-common/src/main/java/io/netty/channel/unix
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/OpenSsl.java +9-3Lines changed: 9 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
35 | 35 | import io.netty.util.internal.logging.InternalLoggerFactory; |
36 | 36 |
|
37 | 37 | import java.io.ByteArrayInputStream; |
| 38 | +import java.nio.ByteBuffer; |
38 | 39 | import java.security.cert.CertificateException; |
39 | 40 | import java.security.cert.X509Certificate; |
40 | 41 | import java.util.ArrayList; |
@@ -667,9 +668,14 @@ static boolean useKeyManagerFactory() { |
667 | 668 |
|
668 | 669 | static long memoryAddress(ByteBuf buf) { |
669 | 670 | assert buf.isDirect(); |
670 | | - return buf.hasMemoryAddress() ? buf.memoryAddress() : |
671 | | - // Use internalNioBuffer to reduce object creation. |
672 | | - Buffer.address(buf.internalNioBuffer(0, buf.readableBytes())); |
| 671 | + if (buf.hasMemoryAddress()) { |
| 672 | + return buf.memoryAddress(); |
| 673 | + } |
| 674 | + // Use internalNioBuffer to reduce object creation. |
| 675 | + // It is important to add the position as the returned ByteBuffer might be shared by multiple ByteBuf |
| 676 | + // instances and so has an address that starts before the start of the ByteBuf itself. |
| 677 | + ByteBuffer byteBuffer = buf.internalNioBuffer(0, buf.readableBytes()); |
| 678 | + return Buffer.address(byteBuffer) + byteBuffer.position(); |
673 | 679 | } |
674 | 680 |
|
675 | 681 | private OpenSsl() { } |
|
Collapse file
Copy file name to clipboardExpand all lines: transport-native-unix-common/src/main/java/io/netty/channel/unix/IovArray.java +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -82,7 +82,12 @@ public IovArray(ByteBuf memory) { |
82 | 82 | memoryAddress = memory.memoryAddress(); |
83 | 83 | } else { |
84 | 84 | // Fallback to using JNI as we were not be able to access the address otherwise. |
85 | | - memoryAddress = Buffer.memoryAddress(memory.internalNioBuffer(0, memory.capacity())); |
| 85 | + |
| 86 | + // Use internalNioBuffer to reduce object creation. |
| 87 | + // It is important to add the position as the returned ByteBuffer might be shared by multiple ByteBuf |
| 88 | + // instances and so has an address that starts before the start of the ByteBuf itself. |
| 89 | + ByteBuffer byteBuffer = memory.internalNioBuffer(0, memory.capacity()); |
| 90 | + memoryAddress = Buffer.memoryAddress(byteBuffer) + byteBuffer.position(); |
86 | 91 | } |
87 | 92 | } |
88 | 93 |
|
|