Skip to content

Commit

Permalink
Fix incorrect string length calculation (#76127)
Browse files Browse the repository at this point in the history
ANSI string depends on system encoding charset. Unix's implementation of Marshal.StringToHGlobalAnsi encodes in UTF8. UTF8 characters which are out of 8-bit range (otcet) encodes in multiple bytes (otcets). ASCII characters usually are in 0x00-0x7F range but cyrillic and other characters are not. For example, 'Зфыы123;' (eq. 'Pass123$') will be encoded in 12 bytes instead of 8

Fix #76125
  • Loading branch information
Sparin committed Sep 27, 2022
1 parent 36bd428 commit 94c6fe6
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ internal static int SearchDirectory(ConnectionHandle ldapHandle, string dn, int
// This option is not supported in Linux, so it would most likely throw.
internal static int SetServerCertOption(ConnectionHandle ldapHandle, LdapOption option, VERIFYSERVERCERT outValue) => Interop.Ldap.ldap_set_option_servercert(ldapHandle, option, outValue);

internal static int BindToDirectory(ConnectionHandle ld, string who, string passwd)
internal static unsafe int BindToDirectory(ConnectionHandle ld, string who, string passwd)
{
IntPtr passwordPtr = IntPtr.Zero;
try
{
passwordPtr = LdapPal.StringToPtr(passwd);
BerVal passwordBerval = new BerVal
{
bv_len = passwd?.Length ?? 0,
bv_len = MemoryMarshal.CreateReadOnlySpanFromNullTerminated((byte*)passwordPtr).Length,
bv_val = passwordPtr,
};

Expand Down

0 comments on commit 94c6fe6

Please sign in to comment.