I was reading Scott Hanselman's The Weekly Source Code 33 - Microsoft Open Source inside Google Chrome and came upon this piece of text:
Older versions of ATL, and by older I mean pre-Visual C++ 2005, used dynamically generated code in small isolated cases. Obviously, without the appropriate APIs this is going to cause problems on a DEP-enabled computer, because you can't execute data. This code is referred to as a "thunk" and versions of ATL in VC++ 2005 and later work correctly with DEP.
I have a great deal of respect towards MS, even though I don't agree with their philosophy. Still, their continuous stride towards "software perfection" is admirable and it helps us all (when they publish their methods / data ;-)). I suspect (haven't verified it) that the given problem arose because the PAGE_EXECUTE flag wasn't passed in that particular version of the library to the memory allocator. If this can happen in a MS library, it can happen to anybody. My conclusions would be:
- When using a function, read the documentation carefully.
- Follow the documentation! Example source is nice, but be very suspicious about it (even if it is provided in the documentation). Preferably use it to orient yourself but don't copy-paste it.
- You're not safe even when using abstraction libraries (this is not to say that they shouldn't be used, just to point out that everybody can make mistakes - although probably any high-quality library contains much lower rates of mistakes than you can achieve).
- When new versions come out (of tools, libraries, etc) - update. Not immediately, but plan for it. It can fix a lot of problems which you never knew you had :-).