View Single Post
  #15  
Old 4th January 2018, 03:19 PM
HatCat's Avatar
HatCat HatCat is offline
Alpha Tester
Project Supporter
Senior Member
 
Join Date: Feb 2007
Location: In my hat.
Posts: 16,260
Default

I wrote a program to dereference user-defined pointer ranges, that would constantly cause an access violation or a segmentation fault.

It goes something like:
Code:
#include <signal.h>
#include <setjmp.h>

int main()
{
...
    signal(SIGSEGV, exception_handler);

    for (i = 0; addr_start + i <= addr_end; i++) {
        unsigned char memory_byte;
        int recovered_from_exception;

        printf("RAM[0x%lX]:  ", addr_start + i);
        recovered_from_exception = setjmp(exc_pt);
        if (recovered_from_exception) {
            signal(SIGSEGV, exception_handler); /* Reschedule the handler. */
            continue; /* Move on to the next byte of memory in loop. */
        }
        memory_byte = *(unsigned char *)(addr_start + i); /* segfault likely */
        printf("0x%02X (%c)\n", memory_byte, memory_byte);
}

void exception_handler(int parameter)
{
    fprintf(stderr, "Exception caught (signal %i).\n", parameter);
    longjmp(exc_pt, parameter);
}
I'm not really understanding what's useful about Project64's C++ try/except/finally that can't be handled portably this way. The only exception (HAHA GET IT?) is the fact the C standard doesn't require all running environments to raise these signals automatically when you try to dereference a NULL pointer or other things. But it does work on both Windows 7 and Linux, and many of the platforms that can't or don't raise the exceptions automatically probably only support the C language and not the C++ language anyway.

Last edited by HatCat; 4th January 2018 at 03:28 PM.
Reply With Quote