I ran into this problem when checking for code containing an unsafe method that returns a pointer.
An example can be expressed as follows:
public class A
{
public static unsafe int* GetAnswer()
{
int fakeValue = 42;
return &(fakeValue);
}
public static void Main()
{
int i = 0;
unsafe { i = *A.GetAnswer(); }
System.Console.WriteLine(i);
}
}
I use two separate validation tools: ILVerify and Peverify.
Steps to play:
- compile sample code with
csc example.cs /t:library /unsafe - check
peverify example.dll - check
ILVerify.exe -r C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll example.dll
Both 2. and 3. will result in the error message below:
[IL]: Error: [C: \ src \ test \ example.dll: A :: GetAnswer ()] [offset 0x00000006] [Int32 address found] Expected numeric type on the stack.
[IL]: Error: [C: \ src \ test \ example.dll: A :: Main ()] [offset 0x00000009] [found Native Int] Expected ByRef value on the stack. 2 Error Checking C: \ src \ test \ example.dll
, , , . - - , ?