4

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

6
  • I would have thought those types of errors would occur when using COM Interop rather then pure .NET development? Anyone know if I'm right in thinking that? Are you talking about the managed heap in .NET though? Commented Oct 22, 2010 at 18:45
  • Is this really a "just curious" question? In that case I vote to close it. There are many questions each of us are "just curious" about, but they aren't all worthy of answers. On the other hand, if you've actually encountered such an exception and need to deal with it, we can talk. Commented Oct 22, 2010 at 18:51
  • I'm looking for pure .NET. I've caused buffer overflows using C++ on purpose (long time back not sure if that is applicable today). Commented Oct 22, 2010 at 18:52
  • Is this related to OWASP, or some other type of security audit... ? Commented Oct 22, 2010 at 18:53
  • Not OWASP related. I had a stackoverflow exception sometime back and thought about how some objects are stored in stack and some in heaps and was just curious if their was a heap / buffer overflow exception in C#. I googled it but couldn't find any answers. Commented Oct 22, 2010 at 18:57

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Depending on what you mean by Buffer overflow, an IndexOutOfRangeException is an exception caused by overflow. You can get it rather easily by accessing an array index beyond its allocation size. Similarly do enough recursion and you can get StackOverflowException. I am not sure about what you're looking for, so you might want to clarify.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.