Validating a Pointer ! Segmentation fault - Unix

This is a discussion on Validating a Pointer ! Segmentation fault - Unix ; AA All, I have a code that does this: char * abc; int a=10,b=20,c=30; abc = (char*) a*b*c; // actually this formula is bit complex than that..but it will do for the example; return strlen(abc); Now, sometimes I get SEG ...

+ Reply to Thread
Results 1 to 9 of 9

Thread: Validating a Pointer ! Segmentation fault

  1. Validating a Pointer ! Segmentation fault

    AA All,

    I have a code that does this:

    char * abc;
    int a=10,b=20,c=30;
    abc = (char*) a*b*c; // actually this formula is bit complex than
    that..but it will do for the example;
    return strlen(abc);



    Now, sometimes I get SEG fault, that's because memory pointed to by
    abc is not always valid, but most of the time it is. What i want to do
    is to check whether abc is pointing valid memory or not (just to
    avaoid exception of SIGSEGV). Check of NULL does not work, as abc
    points to something, but is it meaningful to me or is it within my
    segment?? How to check whether a pointer is a valid one?

    thanks,
    mudassir.


  2. Re: Validating a Pointer ! Segmentation fault

    mudassir writes:

    > AA All,
    >
    > I have a code that does this:
    >
    > char * abc;
    > int a=10,b=20,c=30;
    > abc = (char*) a*b*c; // actually this formula is bit complex than
    > that..but it will do for the example;
    > return strlen(abc);
    >
    > Now, sometimes I get SEG fault, that's because memory pointed to by
    > abc is not always valid, but most of the time it is. What i want to do
    > is to check whether abc is pointing valid memory or not (just to
    > avaoid exception of SIGSEGV). Check of NULL does not work, as abc
    > points to something, but is it meaningful to me or is it within my
    > segment?? How to check whether a pointer is a valid one?


    There is no way of checking a pointer, and neither should there be.
    Properly written code does not produce invalid pointers.

    --
    Måns Rullgård
    mans@mansr.com

  3. Re: Validating a Pointer ! Segmentation fault

    On Jan 11, 9:00 am, mudassir wrote:
    > AA All,
    >
    > I have a code that does this:
    >
    > char * abc;
    > int a=10,b=20,c=30;
    > abc = (char*) a*b*c; // actually this formula is bit complex than
    > that..but it will do for the example;
    > return strlen(abc);
    >
    > Now, sometimes I get SEG fault, that's because memory pointed to by
    > abc is not always valid, but most of the time it is. What i want to do
    > is to check whether abc is pointing valid memory or not (just to
    > avaoid exception of SIGSEGV). Check of NULL does not work, as abc
    > points to something, but is it meaningful to me or is it within my
    > segment?? How to check whether a pointer is a valid one?


    There is no way.


  4. Re: Validating a Pointer ! Segmentation fault

    A little search on Wikipedia unveils a possible solution:

    SIGSEGV can be caught; that is, applications can request what action
    they want to occur in place of the default. Examples of such action
    might be ignoring it, calling a function, or restoring the default
    action. In some circumstances, ignoring SIGSEGV results in undefined
    behavior

  5. Re: Validating a Pointer ! Segmentation fault

    Måns Rullgård writes:
    > mudassir writes:
    >> AA All,
    >>
    >> I have a code that does this:
    >>
    >> char * abc;
    >> int a=10,b=20,c=30;
    >> abc = (char*) a*b*c; // actually this formula is bit complex than
    >> that..but it will do for the example;
    >> return strlen(abc);
    >>
    >> Now, sometimes I get SEG fault, that's because memory pointed to by
    >> abc is not always valid, but most of the time it is. What i want to do
    >> is to check whether abc is pointing valid memory or not (just to
    >> avaoid exception of SIGSEGV). Check of NULL does not work, as abc
    >> points to something, but is it meaningful to me or is it within my
    >> segment?? How to check whether a pointer is a valid one?

    >
    > There is no way of checking a pointer,


    As recently posted in one of the Linux development groups, there is:
    Execute a system call which takes a pointer to some area in userpace
    memory as one if its arguments (eg write, poll) and see if it fails
    with EFAULT.

  6. Re: Validating a Pointer ! Segmentation fault

    Rainer Weikusat writes:

    > Måns Rullgård writes:
    >> mudassir writes:
    >>> AA All,
    >>>
    >>> I have a code that does this:
    >>>
    >>> char * abc;
    >>> int a=10,b=20,c=30;
    >>> abc = (char*) a*b*c; // actually this formula is bit complex than
    >>> that..but it will do for the example;
    >>> return strlen(abc);
    >>>
    >>> Now, sometimes I get SEG fault, that's because memory pointed to by
    >>> abc is not always valid, but most of the time it is. What i want to do
    >>> is to check whether abc is pointing valid memory or not (just to
    >>> avaoid exception of SIGSEGV). Check of NULL does not work, as abc
    >>> points to something, but is it meaningful to me or is it within my
    >>> segment?? How to check whether a pointer is a valid one?

    >>
    >> There is no way of checking a pointer,

    >
    > As recently posted in one of the Linux development groups, there is:
    > Execute a system call which takes a pointer to some area in userpace
    > memory as one if its arguments (eg write, poll) and see if it fails
    > with EFAULT.


    This is a trick that those with legitimate reasons probably already
    know, or at any rate are capable of finding out. There's of course no
    harm mentioning it, but when someone who obviously doesn't need it is
    asking, it is much more helpful to educate them, explaining that they
    should rather be fixing the cause of the bad pointer than carefully
    skirting crashes it might cause.

    --
    Måns Rullgård
    mans@mansr.com

  7. Re: Validating a Pointer ! Segmentation fault

    I disagree with you. Hence my answer.

  8. Re: Validating a Pointer ! Segmentation fault

    Rainer Weikusat wrote:
    > Måns Rullgård writes:
    >> mudassir writes:
    >>> AA All,
    >>>
    >>> I have a code that does this:
    >>>
    >>> char * abc;
    >>> int a=10,b=20,c=30;
    >>> abc = (char*) a*b*c; // actually this formula is bit complex than
    >>> that..but it will do for the example;
    >>> return strlen(abc);
    >>>
    >>> Now, sometimes I get SEG fault, that's because memory pointed to by
    >>> abc is not always valid, but most of the time it is. What i want to do
    >>> is to check whether abc is pointing valid memory or not (just to
    >>> avaoid exception of SIGSEGV). Check of NULL does not work, as abc
    >>> points to something, but is it meaningful to me or is it within my
    >>> segment?? How to check whether a pointer is a valid one?


    >> There is no way of checking a pointer,


    > As recently posted in one of the Linux development groups, there is:
    > Execute a system call which takes a pointer to some area in userpace
    > memory as one if its arguments (eg write, poll) and see if it fails
    > with EFAULT.


    That's true, but it still doesn't work for the original poster's
    question. The reason is that even if the pointer abc can be shown
    to be valid (in that it can be dereferenced without crashing),
    the strlen() could still fail. There might not be a null terminator
    at any of the addresses strlen() will visit before it gets to an
    invalid address.

    - Logan

  9. Re: Validating a Pointer ! Segmentation fault

    Logan Shaw writes:

    > Rainer Weikusat wrote:
    >> Måns Rullgård writes:
    >>> mudassir writes:
    >>>> AA All,
    >>>>
    >>>> I have a code that does this:
    >>>>
    >>>> char * abc;
    >>>> int a=10,b=20,c=30;
    >>>> abc = (char*) a*b*c; // actually this formula is bit complex than
    >>>> that..but it will do for the example;
    >>>> return strlen(abc);
    >>>>
    >>>> Now, sometimes I get SEG fault, that's because memory pointed to by
    >>>> abc is not always valid, but most of the time it is. What i want to do
    >>>> is to check whether abc is pointing valid memory or not (just to
    >>>> avaoid exception of SIGSEGV). Check of NULL does not work, as abc
    >>>> points to something, but is it meaningful to me or is it within my
    >>>> segment?? How to check whether a pointer is a valid one?

    >
    >>> There is no way of checking a pointer,

    >
    >> As recently posted in one of the Linux development groups, there is:
    >> Execute a system call which takes a pointer to some area in userpace
    >> memory as one if its arguments (eg write, poll) and see if it fails
    >> with EFAULT.

    >
    > That's true, but it still doesn't work for the original poster's
    > question. The reason is that even if the pointer abc can be shown
    > to be valid (in that it can be dereferenced without crashing),
    > the strlen() could still fail. There might not be a null terminator
    > at any of the addresses strlen() will visit before it gets to an
    > invalid address.


    More to the point, a pointer being within the address space of a
    process, doesn't necessarily make it correct. A bad pointer caused by
    a bug is at least as likely (probably quite a bit more likely) to be
    within the address space as outside it. Knowing that a pointer is in
    the process address space is hardly ever useful at all.

    --
    Måns Rullgård
    mans@mansr.com

+ Reply to Thread