Page 1 of 1

2001: unsafe method exception

Posted: Thu Mar 07, 2024 3:45 pm
by weloveayaka

Code: Select all

[Obfuscation(Feature = "ultra", Exclude = false)]
public unsafe class UnsafeClass
{
    
    public void UnsafeMethod()
    {
        int* ptr = stackalloc int[1];
        *ptr = 42;
        Console.WriteLine("Unsafe method: " + *ptr);
    }
}
 
Type “System.IntPtr” cannot cast to “System.Int32&”

Below works fine.

Code: Select all

public static unsafe void unsafe2()
        {
            Console.WriteLine("Unsafe2");
            int* pointer = stackalloc int[10];
            for (int i = 0; i < 10; i++)
            {
                pointer[i] = i;
            }
            Console.WriteLine($"Stackalloc result: {pointer[5]}");
        }

Re: 2001: unsafe method exception

Posted: Thu Mar 07, 2024 4:07 pm
by weloveayaka

Code: Select all

 private static unsafe int PerformUnsafeOperation(int[] numbers)
        {
            fixed (int* pointer = numbers)
            {
                int sum = 0;
                for (int i = 0; i < numbers.Length; i++)
                {
                    sum += pointer[i];
                }
                return sum;
            }
        }
 
This one gets NullPointerException

P.S.
This Exception can't decode Stack trace using map file.
Only the "main" function's name decoded

Re: 2001: unsafe method exception

Posted: Sat Mar 16, 2024 7:39 am
by Admin
Fixed in the 2023 build except this:

Code: Select all

public unsafe class UnsafeClass
{
    
    public void UnsafeMethod()
    {
        int* ptr = stackalloc int[1];
        *ptr = 42;
        Console.WriteLine("Unsafe method: " + *ptr);
    }
}