c++dep

How can I create simple C++ code that runs fine with no Data Execution Prevention (DEP) but will crash with DEP on?


While I understand code that is not marked "executable" will trigger a DEP crash, I am trying to understand what type of common coding practices (in legacy Windows apps) would result in this type of crash.


Solution

  • Something like this:

    
    int main()
    {
        char* s = (char*)malloc(1);
        s[0] = '\xC3';
        void (*p)() = (void (*)())(s);
        p();
    }
    

    ATL did this to allocate thunk for WndProc. The purpose of such WndProc thunks is to embed context parameter and use a method for WndProc instead of a function not taking extra context parameter.

    The fix is easy enough, and does not necessarily include removal of dynamic code allocation: