Skip to content

Commit ddb50fc

Browse files
committed
add 4-byte alignment
1 parent 7392b08 commit ddb50fc

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/libraries/Common/src/System/Net/SocketAddress.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ public SocketAddress(AddressFamily family, int size)
9494
InternalSize = size;
9595
#if !SYSTEM_NET_PRIMITIVES_DLL && WINDOWS
9696
// WSARecvFrom needs a pinned pointer to the 32bit socket address size.
97-
// Allocate extra bytes at the end of Buffer, so we don't need to pin anything else.
98-
size += sizeof(int);
97+
// Allocate extra bytes at the end of Buffer with a 4-byte alignment, so we don't need to pin anything else.
98+
// The following forumla ensures addition of the minimum necessary extra padding,
99+
// eg. size=16 will be extended to 20, while size=17 will be extended to 24
100+
const int PtrSize = sizeof(int);
101+
size = (size + PtrSize - 1) / PtrSize * PtrSize + PtrSize;
99102
#endif
100103
Buffer = new byte[size];
101104

0 commit comments

Comments
 (0)