Determining if an address is valid prior to using it.

Nathan Weyer nweyer@meritgames.com
Wed Jul 23 20:34:00 GMT 2008


This is the only way I am aware of.



---------------------------------------------------------------
bool            AddyIsOk;
u32*            JunkAddy;
void*            JunkInfo;
sigjmp_buf      test_sigjump_buf;

void test_signal_fxn(int signo)
{
//      REPORT("Address test failed!\n");
        AddyIsOk = false;
        JunkAddy = (u32*)&JunkAddy;
        siglongjmp(test_sigjump_buf,1);
}

bool debug_check_address(void* addy)
{
//    fprintf(stderr,"Testing [%p]\n",addy);
        struct sigaction test_sig;
        struct sigaction save_sig;
        AddyIsOk = true;

        test_sig.sa_handler = test_signal_fxn;
        sigemptyset(&test_sig.sa_mask);
        test_sig.sa_flags = 0;

        JunkAddy = (u32*)addy;

        if(sigaction(SIGSEGV,&test_sig,&save_sig) == -1)
        {
                perror("Could Not Install TestSig : ");
                return false;
        }

        if(sigsetjmp(test_sigjump_buf,1) == 0)
        {
            JunkInfo    =   (void*)(*JunkAddy);
            JunkInfo    =   (void*)(((u32)(JunkInfo))+4);
        }

        if(sigaction(SIGSEGV,&save_sig,NULL) == -1)
        {
                perror("Could Not Revert Sig : ");
                return false;
        }

//    fprintf(stderr,"[%p] is [%s]\n",addy,AddyIsOk?"Ok":"Bad");
        return AddyIsOk;


}

----- Original Message -----
From: "jimmie davis" <jimmie.davis@l-3com.com>
To: libc-help@sourceware.org
Sent: Wednesday, July 23, 2008 4:06:00 PM GMT -05:00 US/Canada Eastern
Subject: Determining if an address is valid prior to using it.

Given an arbitrary address, is it possible to test and see if it is in
the process's address space ?

My current technique is to create a new thread, then access the memory
from that thread.  Then if it did not segfault i know the address was
valid.


Is there a better way to do this ?


thanks,
bud davis



"Buy the ticket, take the ride."
                   Hunter S. Thompson



More information about the Libc-help mailing list