none
Using /clr imply /EHa but an AV exception is not catched like msdn docs said... RRS feed

  • Question

  • Hi all,

    Maybe someone could explain better this behavior?

    Tool used: MS VS2010

    Project: console win32, with option /CLR (win32: release / debug)

    version of CLR: v4.0

    Problem: according to the msdn docs (see: Exception-Handling Models and Compiler Options), we have that "The /CLR compiler options imply specification of the /EHa option."

    Near that text we have a security alert note saying: "You expose yourself to a security threat if you use the /EHa or /CLR compiler option and a catch statement with an ellipsis exception-declaration (catch(...)). "

    So using /EHa implies that you will be able to catch including access violation exceptions (AV) using catch (...). And it is right only with you don't use /CLR (/noCLR) and use /EHa.

    But unlike, using /CLR and /EHa the AV will terminate the program. What happens behind scenes with CLR? In my understanding and as said in docs it should be able to catch (...)

    The code block illustrates the example:

    try
    {
      unsigned int* p = (unsigned int*)-1;
      *p = 2;
    }
    catch (...)
    {
      _tprintf(_T("AV catch...\n"));
    }

    As said "if you use the /EHa or /CLR compiler option and a catch statement with ....". The clause or, is not that right, because when you use /EHa and /CLR  the AV is not caught.

    So, what's the CLR spec says on how it should work?

    Thanks!

    Tuesday, August 21, 2012 7:34 PM

All replies

  • Hi Tdrbr,

    Welcome to the MSDN Forum.

    I have tried to involving some other one into this thread.

    Please be patient.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 22, 2012 9:05 AM
    Moderator
  • Hi Mike,

    No problem waiting...

    If interested here are links for a more detailed coverage made:

    PDF:

    Test Exceptions Full.pdf

    VS Project:

    ExceptionTestDeploy.zip

    Best regards,

    Thomas



    • Edited by tdrbr Wednesday, August 22, 2012 1:01 PM
    Wednesday, August 22, 2012 1:00 PM
  • "So using /EHa implies that you will be able to catch including access violation exceptions (AV) using catch (...)."

    Yes but ultimately the CLR decides, not the compiler. Starting with .NET 4 such exceptions cannot be caught by default.

    If you really need (in 99.99% cases catching such exceptions is a bad idea) to catch an AccessViolationException then you can add the HandleProcessCorruptedStateExceptions attribute to the method in question or you can enable this in the .config file (see http://msdn.microsoft.com/en-us/library/dd638517.aspx).

    Saturday, August 25, 2012 9:53 AM
    Moderator