Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C/C++ User's Guide - why would you want to resume execution of the instruction that caused the http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Why would you want to resume execution of the instruction that caused the
exception?  Since the exception filter can involve a function call, that
function can attempt to correct the problem.  For example, if it is
determined that the exception has occurred because of the NULL pointer
dereference, the function could modify the pointer so that it is no longer
NULL.

Example:

     #include <stdio.h>
     #include <stdlib.h>
     #include <excpt.h>

     char *NullP = NULL;


     int filter()
     {
       if( NullP == NULL ) {
         NullP == malloc( 20 );
         return( EXCEPTION_CONTINUE_EXECUTION )
       }
       return( EXCEPTION_EXECUTE_HANDLER )
     }


     void main( int argc, char **argv )
     {

       printf( "Attempting illegal memory reference.\n" );
       _try {
         *NullP = '\1';
       }


       _except (filter()) {
         printf( "Oh no! We had an exception!\n" );
       }
       printf( "We recovered fine...\n" );
     }

Unfortunately, this is does not solve the problem.  Understanding why it
does not involves looking at the sequence of computer instructions that is
generated for the expression in question.


         *NullP = '\1';
             mov     eax,dword ptr _NullP
             mov     byte ptr [eax],01H

The exception is caused by the second instruction which contains a pointer
to the referenced memory location (i.e., 0) in register EAX.  This is the
instruction that will be repeated when the filter returns
EXCEPTION_CONTINUE_EXECUTION.  Since EAX did not get changed by our fix, the
exception will reoccur.  Fortunately, NullP is changed and this prevents our
program from looping forever.  The moral here is that there are very few
instances where you can correct "on the fly" a problem that is causing an
exception to occur.  Certainly, any attempt to do so must involve a careful
inspection of the computer instruction sequence that is generated by the
compiler (and this sequence usually varies with the selection of compiler
optimization options).  The best solution is to add some more code to detect
the problem before the exception occurs.

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson