From ddb50fcecb2baeb8d5215d7e835399a12b984e65 Mon Sep 17 00:00:00 2001 From: antonfirsov Date: Wed, 30 Nov 2022 01:27:27 +0100 Subject: [PATCH] add 4-byte alignment --- src/libraries/Common/src/System/Net/SocketAddress.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libraries/Common/src/System/Net/SocketAddress.cs b/src/libraries/Common/src/System/Net/SocketAddress.cs index 8a1ea3cf064336..a647b5ba5ef57d 100644 --- a/src/libraries/Common/src/System/Net/SocketAddress.cs +++ b/src/libraries/Common/src/System/Net/SocketAddress.cs @@ -94,8 +94,11 @@ public SocketAddress(AddressFamily family, int size) InternalSize = size; #if !SYSTEM_NET_PRIMITIVES_DLL && WINDOWS // WSARecvFrom needs a pinned pointer to the 32bit socket address size. - // Allocate extra bytes at the end of Buffer, so we don't need to pin anything else. - size += sizeof(int); + // Allocate extra bytes at the end of Buffer with a 4-byte alignment, so we don't need to pin anything else. + // The following forumla ensures addition of the minimum necessary extra padding, + // eg. size=16 will be extended to 20, while size=17 will be extended to 24 + const int PtrSize = sizeof(int); + size = (size + PtrSize - 1) / PtrSize * PtrSize + PtrSize; #endif Buffer = new byte[size];