0

I'm reading about unsafe code, primarily for working with Bitmaps. I can't find, however, an explanation of the limits of unsafeness. I understand that using pointers on an array will not be checked so I might try accessing memory outside of its bounds. So far so good. But what if I do access that memory. What happens? Might I be changing other variables' values? Or even change the program's binary code? Or just raise an exception?

I'd like to know what's the worst that can happen before I decide to use unsafe code.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • 2
    Undefined behavior. You can't predict what might happen with any degree of certainty without knowing the exact context in which your program is running. – itsme86 Jun 22 '16 at 19:38
  • @itsme86 Thanks. I assume, though, that there are _some_ known limits. For example - my code can't access memory that's reserved for other processes on the computer, so it can't crash other programs or the operating system. Right? (Not including using up resources. I mean - it can't alter their memory space. Is that correct?) – ispiro Jun 22 '16 at 19:42
  • Would this question be more appropriate for [Programmers StackExhcange](http://programmers.stackexchange.com/) ? Otherwise, a good question :) – Roman Jun 22 '16 at 19:48
  • Thanks. But I'm not asking "In what situations should I use unsafe code?". I'm asking about the actual results of unsafe bugs. – ispiro Jun 22 '16 at 19:50
  • The same things that can happen when ordinary C code runs off the rails... Bad things, up to and including crashes. – Robert Harvey Jun 22 '16 at 19:51
  • @RobertHarvey Thanks. Not being a **C** programmer, I wonder if there are limits to that. Corrupting other processes? The OS? – ispiro Jun 22 '16 at 19:53
  • (sorry for C not C#) Early hackers discovered they could use unsafe memory to rewrite data within C-language stack frames, potentially gaining command-line root access to the target system. [This article in Phrack](http://phrack.org/issues/49/14.html) famously details these "stack smashing" attacks. There are security measures commonly put in place to avoid such problems now, but the point I wish to get across here is that where ingenuity and unsafe memory meet, there is bound to be trouble. Tread carefully. – Conduit Jun 22 '16 at 19:54
  • I think it's safe to say that you can hose the app domain. I'm not sure if you can cause damage outside of that; usually your application will terminate if you try to access memory outside of the app domain. – Robert Harvey Jun 22 '16 at 19:54
  • @RobertHarvey Thanks. – ispiro Jun 22 '16 at 19:55
  • `unsafe` code is fine if you're careful with it. I use it for very specific optimizations, like [this one](http://stackoverflow.com/questions/11660127/faster-way-to-swap-endianness-in-c-sharp-with-32-bit-words). – Robert Harvey Jun 22 '16 at 19:57
  • @Roman when referring other sites, it is often helpful to point that [cross-posting is frowned upon](http://meta.stackexchange.com/tags/cross-posting/info) – gnat Jun 22 '16 at 19:58
  • In general, for normal applications, the operating system will make sure that they cannot access the memory outside of their own process. And if they try, they will usually crash. – poke Jun 22 '16 at 20:04
  • @gnat, That is a very good point, thank you :) I was thinking more of a migrating the question, rather than adding it to another site as well. – Roman Jun 23 '16 at 13:43

1 Answers1

-1

So unsafe code could lead to memory leaks or other random errors. Unsafe code is unsafe for a reason. For example in C++ you had pointers and those were basically pointing to chunks of memory on the physical machine. There are so many different things that could go wrong with a pointer that it is best not to use them if you aren't sure what you are doing. I think the same thing can be said of unsafe code in C#. itsme86 is right it is very unpredictable.

jt25617
  • 21
  • 6