Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heap / buffer overflow exception

Just curious, Is there or has anyone ever come across a heap / buffer overflow exception in C#?

like image 405
Viv Avatar asked Jan 22 '23 05:01

Viv


1 Answers

You can cause a buffer overflow in C# in unsafe code. For example:

public unsafe struct testo
{
    public int before;
    public fixed int items[16];
    public int after;
}

testo x = new testo();
x.after = 1;
for (int i = 0; i <= 16; ++i)
{
    unsafe
    {
        x.items[i] = 99;
     }
}
Console.WriteLine(x.after);

The above will print "99" because it overflowed the buffer.

Absent unsafe code, I do not know of any way to cause a buffer overrun that doesn't trigger an exception.

like image 136
Jim Mischel Avatar answered Jan 23 '23 19:01

Jim Mischel