Merge pull request #3921 from shrah/null_check

Add null check for inline string marshalling
This commit is contained in:
Faizur Rahman 2017-06-20 16:09:53 -07:00 коммит произвёл GitHub
Родитель 25774e186f 9df11fa0ca
Коммит 6ea23dec10
4 изменённых файлов: 42 добавлений и 3 удалений

Просмотреть файл

@ -1902,11 +1902,18 @@ namespace Internal.TypeSystem.Interop
codeStream.EmitLdArg(0);
codeStream.Emit(ILOpcode.ldfld, emitter.NewToken(_managedField));
var lNullCheck = emitter.NewCodeLabel();
codeStream.Emit(ILOpcode.brfalse, lNullCheck);
codeStream.EmitLdArg(0);
codeStream.Emit(ILOpcode.ldfld, emitter.NewToken(_managedField));
codeStream.Emit(ILOpcode.ldlen);
codeStream.Emit(ILOpcode.conv_i4);
codeStream.EmitStLoc(vLength);
codeStream.EmitLabel(lNullCheck);
Debug.Assert(MarshalAsDescriptor.SizeConst.HasValue);
int sizeConst = (int)MarshalAsDescriptor.SizeConst.Value;
@ -1969,8 +1976,6 @@ namespace Internal.TypeSystem.Interop
// check if ManagedType == null, then return
codeStream.EmitLdArg(0);
codeStream.Emit(ILOpcode.ldfld, emitter.NewToken(_managedField));
codeStream.Emit(ILOpcode.ldnull);
codeStream.Emit(ILOpcode.cgt_un);
codeStream.Emit(ILOpcode.brfalse, lDone);
codeStream.EmitLdArg(1);

Просмотреть файл

@ -42,6 +42,12 @@ namespace Internal.Runtime.CompilerHelpers
if (buffer == null)
return;
if (str == null)
{
buffer[0] = '\0';
return;
}
Debug.Assert(str.Length >= length);
fixed (char* pStr = str)

Просмотреть файл

@ -187,6 +187,18 @@ namespace PInvokeTests
[DllImport("*", CallingConvention = CallingConvention.StdCall)]
internal static extern IntPtr GetFunctionPointer();
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal unsafe struct InlineString
{
internal uint size;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
internal string name;
}
[DllImport("*", CallingConvention = CallingConvention.StdCall)]
static extern bool InlineStringTest(ref InlineString ias);
public static int Main(string[] args)
{
TestBlittableType();
@ -591,6 +603,10 @@ namespace PInvokeTests
}
ThrowIfNotEquals(true, StructTest_Array(ssa, ssa.Length), "Array of struct marshalling failed");
InlineString ils = new InlineString();
InlineStringTest(ref ils);
ThrowIfNotEquals("Hello World!", ils.name, "Inline string marshalling failed");
InlineArrayStruct ias = new InlineArrayStruct();
ias.inlineArray = new short[128];

Просмотреть файл

@ -628,6 +628,18 @@ DLL_EXPORT void* __stdcall GetFunctionPointer()
{
return (void*)&SetLastErrorFunc;
}
typedef struct {
int c;
char inlineString[260];
} inlineString;
DLL_EXPORT bool __stdcall InlineStringTest(inlineString* p)
{
CopyAnsiString(p->inlineString, "Hello World!");
return true;
}
#if (_MSC_VER >= 1400) // Check MSC version
#pragma warning(pop) // Renable previous depreciations
#endif