Skip to content

Commit

Permalink
Use more specific types in Interop.WideCharToMultiByte signature (#69338
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jkotas committed May 14, 2022
1 parent d578d78 commit e1bcd56
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 24 deletions.
Expand Up @@ -230,6 +230,8 @@ internal static unsafe nuint GetRawObjectDataSize(object obj)
return rawSize;
}

// Returns array element size.
// Callers are required to keep obj alive
internal static unsafe ushort GetElementSize(this Array array)
{
Debug.Assert(ObjectHasComponentSize(array));
Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs
Expand Up @@ -140,7 +140,7 @@ internal static unsafe void ConvertFixedToNative(int flags, string strManaged, I
// Flags defined in ILFixedCSTRMarshaler::EmitConvertContentsCLRToNative(ILCodeStream* pslILEmit).
bool throwOnUnmappableChar = 0 != (flags >> 8);
bool bestFit = 0 != (flags & 0xFF);
uint defaultCharUsed = 0;
Interop.BOOL defaultCharUsed = Interop.BOOL.FALSE;

int cbWritten;

Expand All @@ -154,14 +154,14 @@ internal static unsafe void ConvertFixedToNative(int flags, string strManaged, I
numChars,
buffer,
length,
IntPtr.Zero,
throwOnUnmappableChar ? new IntPtr(&defaultCharUsed) : IntPtr.Zero);
null,
throwOnUnmappableChar ? &defaultCharUsed : null);
#else
cbWritten = Encoding.UTF8.GetBytes(pwzChar, numChars, buffer, length);
#endif
}

if (defaultCharUsed != 0)
if (defaultCharUsed != Interop.BOOL.FALSE)
{
throw new ArgumentException(SR.Interop_Marshal_Unmappable_Char);
}
Expand Down
Expand Up @@ -38,9 +38,8 @@ public static unsafe int ConvertWideCharToMultiByte(char* wideCharStr, int wideC
wideCharLen,
multiByteStr,
multiByteLen,
default(IntPtr),
default(IntPtr)
);
null,
null);
}

// Convert a UTF16 string to ANSI byte array using flags
Expand All @@ -52,17 +51,16 @@ public static unsafe int ConvertWideCharToMultiByte(char* wideCharStr, int wideC
bool throwOnUnmappableChar)
{
uint flags = (bestFit ? 0 : Interop.Kernel32.WC_NO_BEST_FIT_CHARS);
int defaultCharUsed = 0;
Interop.BOOL defaultCharUsed = Interop.BOOL.FALSE;
int ret = Interop.Kernel32.WideCharToMultiByte(Interop.Kernel32.CP_ACP,
flags,
wideCharStr,
wideCharLen,
multiByteStr,
multiByteLen,
default(IntPtr),
throwOnUnmappableChar ? new System.IntPtr(&defaultCharUsed) : default(IntPtr)
);
if (defaultCharUsed != 0)
null,
throwOnUnmappableChar ? &defaultCharUsed : null);
if (defaultCharUsed != Interop.BOOL.FALSE)
{
throw new ArgumentException(SR.Interop_Marshal_Unmappable_Char);
}
Expand All @@ -79,9 +77,8 @@ public static unsafe int GetByteCount(char* wStr, int wideStrLen)
wideStrLen,
default(byte*),
0,
default(IntPtr),
default(IntPtr)
);
null,
null);
}

// Return number of charaters encoded in native byte array lpMultiByteStr
Expand Down
Expand Up @@ -13,7 +13,7 @@ internal static partial class Kernel32
uint CodePage, uint dwFlags,
char* lpWideCharStr, int cchWideChar,
byte* lpMultiByteStr, int cbMultiByte,
IntPtr lpDefaultChar, IntPtr lpUsedDefaultChar);
byte* lpDefaultChar, BOOL* lpUsedDefaultChar);

internal const uint CP_ACP = 0;
internal const uint WC_NO_BEST_FIT_CHARS = 0x00000400;
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/Text/OSEncoding.Windows.cs
Expand Up @@ -235,7 +235,7 @@ public override Decoder GetDecoder()

internal static unsafe int WideCharToMultiByte(int codePage, char* pChars, int count, byte* pBytes, int byteCount)
{
int result = Interop.Kernel32.WideCharToMultiByte((uint)codePage, 0, pChars, count, pBytes, byteCount, IntPtr.Zero, IntPtr.Zero);
int result = Interop.Kernel32.WideCharToMultiByte((uint)codePage, 0, pChars, count, pBytes, byteCount, null, null);
if (result <= 0)
throw new ArgumentException(SR.Argument_InvalidCharSequenceNoIndex);
return result;
Expand Down
Expand Up @@ -32,7 +32,7 @@ private static unsafe void WriteCore(IntPtr handle, string s)
{
cbytes = Interop.Kernel32.WideCharToMultiByte(
Interop.Kernel32.GetConsoleOutputCP(),
0, pChars, s.Length, pBytes, bytes.Length, IntPtr.Zero, IntPtr.Zero);
0, pChars, s.Length, pBytes, bytes.Length, null, null);
}

fixed (byte* pBytes = bytes)
Expand Down
Expand Up @@ -56,7 +56,7 @@ internal static unsafe int StringToAnsiString(string s, byte* buffer, int buffer
int nb;

uint flags = bestFit ? 0 : Interop.Kernel32.WC_NO_BEST_FIT_CHARS;
uint defaultCharUsed = 0;
Interop.BOOL defaultCharUsed = Interop.BOOL.FALSE;

fixed (char* pwzChar = s)
{
Expand All @@ -67,11 +67,11 @@ internal static unsafe int StringToAnsiString(string s, byte* buffer, int buffer
s.Length,
buffer,
bufferLength,
IntPtr.Zero,
throwOnUnmappableChar ? new IntPtr(&defaultCharUsed) : IntPtr.Zero);
null,
throwOnUnmappableChar ? &defaultCharUsed : null);
}

if (defaultCharUsed != 0)
if (defaultCharUsed != Interop.BOOL.FALSE)
{
throw new ArgumentException(SR.Interop_Marshal_Unmappable_Char);
}
Expand All @@ -94,7 +94,7 @@ internal static unsafe int GetAnsiStringByteCount(ReadOnlySpan<char> chars)
fixed (char* pChars = chars)
{
byteLength = Interop.Kernel32.WideCharToMultiByte(
Interop.Kernel32.CP_ACP, Interop.Kernel32.WC_NO_BEST_FIT_CHARS, pChars, chars.Length, null, 0, IntPtr.Zero, IntPtr.Zero);
Interop.Kernel32.CP_ACP, Interop.Kernel32.WC_NO_BEST_FIT_CHARS, pChars, chars.Length, null, 0, null, null);
if (byteLength <= 0)
throw new ArgumentException();
}
Expand All @@ -118,7 +118,7 @@ internal static unsafe void GetAnsiStringBytes(ReadOnlySpan<char> chars, Span<by
fixed (byte* pBytes = bytes)
{
byteLength = Interop.Kernel32.WideCharToMultiByte(
Interop.Kernel32.CP_ACP, Interop.Kernel32.WC_NO_BEST_FIT_CHARS, pChars, chars.Length, pBytes, bytes.Length, IntPtr.Zero, IntPtr.Zero);
Interop.Kernel32.CP_ACP, Interop.Kernel32.WC_NO_BEST_FIT_CHARS, pChars, chars.Length, pBytes, bytes.Length, null, null);
if (byteLength <= 0)
throw new ArgumentException();
}
Expand Down

0 comments on commit e1bcd56

Please sign in to comment.