verifying valid Pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andreas

    verifying valid Pointer

    Hello Newsgroup,

    I've got a problem with a function that should return a pointer to the
    beginning of a string or NULL. like this:

    char* getString(void) ;

    The problem is, that the function seems to be bugy. It sometimes returns a
    pointer, that must not be derefered, if I try, I get a segmentation fault
    or something like this. This pointer is not NULL.
    How can I test, if I can access the String whithout a programm crash? A try
    catch block does not work.

    Just to illustrate:

    char* getString(void) { //this is just to create a bad return
    char* null = NULL; //the real bugy function is in a library
    return &null[1]; //i have to use
    }

    int main(void) {
    char* badPtr = getString();
    if(badPtr == NULL) {
    printf("NULL Pointer\n"); // this does not happen
    } else {
    printf("Not NULL Pointer\n");
    printf("Value: %i\n",*badPtr); // program crashes here
    }
    }

    I had this Problems under Windows whis cygwin/gcc and borland bcc.
    I think i can imagine why, but how can i solve it?

    thanks
  • GanetMarV

    #2
    Re: verifying valid Pointer

    return &null[1]; won't work
    return &null[0]; will.

    Comment

    • andreas

      #3
      Re: verifying valid Pointer

      GanetMarV wrote:
      [color=blue]
      > return &null[1]; won't work
      > return &null[0]; will.[/color]

      that's not the Point.
      I can't solve it in getString, cause this is not my code.
      I have to test the pointer in main

      thanks anyway

      Comment

      • Mike Wahler

        #4
        Re: verifying valid Pointer


        "andreas" <andreas_neu@gm x.de> wrote in message
        news:41ed63e5$0 $25943$9b622d9e @news.freenet.d e...[color=blue]
        > Hello Newsgroup,
        >
        > I've got a problem with a function that should return a pointer to the
        > beginning of a string or NULL. like this:
        >
        > char* getString(void) ;
        >
        > The problem is, that the function seems to be bugy. It sometimes returns a
        > pointer, that must not be derefered, if I try, I get a segmentation fault
        > or something like this. This pointer is not NULL.
        > How can I test, if I can access the String whithout a programm crash? A[/color]
        try[color=blue]
        > catch block does not work.
        >
        > Just to illustrate:
        >
        > char* getString(void) { //this is just to create a bad return
        > char* null = NULL; //the real bugy function is in a library
        > return &null[1]; //i have to use[/color]

        Change this to:

        return null;

        [color=blue]
        > }
        >
        > int main(void) {
        > char* badPtr = getString();
        > if(badPtr == NULL) {
        > printf("NULL Pointer\n"); // this does not happen
        > } else {
        > printf("Not NULL Pointer\n");
        > printf("Value: %i\n",*badPtr); // program crashes here
        > }
        > }
        >
        > I had this Problems under Windows whis cygwin/gcc and borland bcc.
        > I think i can imagine why, but how can i solve it?[/color]

        See above, and read up about arrays and pointers.

        -Mike


        Comment

        • Mike Wahler

          #5
          Re: verifying valid Pointer


          "GanetMarV" <ganetmarv@gmai l.com> wrote in message
          news:1106076999 .288045.16040@c 13g2000cwb.goog legroups.com...[color=blue]
          > return &null[1]; won't work
          > return &null[0]; will.[/color]

          No.

          return null;

          -Mike


          Comment

          • Andrew Koenig

            #6
            Re: verifying valid Pointer

            "andreas" <andreas_neu@gm x.de> wrote in message
            news:41ed63e5$0 $25943$9b622d9e @news.freenet.d e...
            [color=blue]
            > How can I test, if I can access the String whithout a programm crash? A
            > try
            > catch block does not work.[/color]

            You can't. By far your best recourse is to find the bug.


            Comment

            • andreas

              #7
              Re: verifying valid Pointer

              Andrew Koenig wrote:
              [color=blue]
              > "andreas" <andreas_neu@gm x.de> wrote in message
              > news:41ed63e5$0 $25943$9b622d9e @news.freenet.d e...
              >[color=green]
              >> How can I test, if I can access the String whithout a programm crash? A
              >> try
              >> catch block does not work.[/color]
              >
              > You can't. By far your best recourse is to find the bug.[/color]

              Ok. there is no function in ANSI/ISO c/c++ to test the pointer. But are you
              shure that there is now other way?
              I can't find the bug myself, cause it's in a Windows API call.

              Comment

              • Julián Albo

                #8
                Re: verifying valid Pointer

                andreas wrote:
                [color=blue]
                > Ok. there is no function in ANSI/ISO c/c++ to test the pointer. But are
                > you shure that there is now other way?
                > I can't find the bug myself, cause it's in a Windows API call.[/color]

                Are you sure is a bug? Sometimes this is used as the documented way to
                report some error by a function (not a very recommended way, by the way).

                --
                Salu2

                Comment

                • Victor Bazarov

                  #9
                  Re: verifying valid Pointer

                  andreas wrote:[color=blue]
                  > Andrew Koenig wrote:
                  >
                  >[color=green]
                  >>"andreas" <andreas_neu@gm x.de> wrote in message
                  >>news:41ed63e5 $0$25943$9b622d 9e@news.freenet .de...
                  >>
                  >>[color=darkred]
                  >>>How can I test, if I can access the String whithout a programm crash? A
                  >>>try
                  >>>catch block does not work.[/color]
                  >>
                  >>You can't. By far your best recourse is to find the bug.[/color]
                  >
                  >
                  > Ok. there is no function in ANSI/ISO c/c++ to test the pointer. But are you
                  > shure that there is now other way?[/color]

                  If there is, it wouldn't be topical in the ANSI/ISO C++ newsgroup,
                  would it?
                  [color=blue]
                  > I can't find the bug myself, cause it's in a Windows API call.[/color]

                  My guess would be that Microsoft is to blame for everything.

                  Comment

                  • Ioannis Vranos

                    #10
                    Re: verifying valid Pointer

                    andreas wrote:
                    [color=blue]
                    > Hello Newsgroup,
                    >
                    > I've got a problem with a function that should return a pointer to the
                    > beginning of a string or NULL. like this:
                    >
                    > char* getString(void) ;
                    >
                    > The problem is, that the function seems to be bugy. It sometimes returns a
                    > pointer, that must not be derefered, if I try, I get a segmentation fault
                    > or something like this. This pointer is not NULL.
                    > How can I test, if I can access the String whithout a programm crash? A try
                    > catch block does not work.
                    >
                    > Just to illustrate:
                    >
                    > char* getString(void) { //this is just to create a bad return
                    > char* null = NULL; //the real bugy function is in a library
                    > return &null[1]; //i have to use
                    > }
                    >
                    > int main(void) {
                    > char* badPtr = getString();
                    > if(badPtr == NULL) {
                    > printf("NULL Pointer\n"); // this does not happen
                    > } else {
                    > printf("Not NULL Pointer\n");
                    > printf("Value: %i\n",*badPtr); // program crashes here
                    > }
                    > }
                    >
                    > I had this Problems under Windows whis cygwin/gcc and borland bcc.
                    > I think i can imagine why, but how can i solve it?[/color]


                    You had better fix the bug.


                    If you have not access to the function source, and you are desperate
                    (you gotta use it), you can write a test routine to start printing the
                    pointer values returned along with dereferencing them to see what kind
                    of pattern the invalid pointers are (if any) e.g. just one specific
                    invalid value, and act accordingly (if possible).


                    But that is completely unsafe.




                    --
                    Ioannis Vranos


                    Comment

                    • Larry Brasfield

                      #11
                      Re: verifying valid Pointer

                      "andreas" <andreas_neu@gm x.de> wrote in message news:41ed70f5$0 $30345$9b622d9e @news.freenet.d e...[color=blue]
                      > Andrew Koenig wrote:
                      >[color=green]
                      >> "andreas" <andreas_neu@gm x.de> wrote in message
                      >> news:41ed63e5$0 $25943$9b622d9e @news.freenet.d e...
                      >>[color=darkred]
                      >>> How can I test, if I can access the String whithout a programm crash? A
                      >>> try
                      >>> catch block does not work.[/color]
                      >>
                      >> You can't. By far your best recourse is to find the bug.[/color]
                      >
                      > Ok. there is no function in ANSI/ISO c/c++ to test the pointer. But are you
                      > shure that there is now other way?[/color]

                      He did not say that. He said your best solution to the
                      problem you face is to find the bug causing the problem.
                      He implied that finding it and fixing it is the best solution.
                      [color=blue]
                      > I can't find the bug myself, cause it's in a Windows API call.[/color]

                      You are confusing "fails during API call" with
                      "bug is in API". Most likely, a bug in your code is causing
                      the problem; it just becomes apparent when that call occurs.

                      --
                      --Larry Brasfield
                      email: donotspam_larry _brasfield@hotm ail.com
                      Above views may belong only to me.


                      Comment

                      • andreas

                        #12
                        Re: verifying valid Pointer

                        > You are confusing "fails during API call" with[color=blue]
                        > "bug is in API". Most likely, a bug in your code is causing
                        > the problem; it just becomes apparent when that call occurs.
                        >[/color]
                        No, I'm not. The program does not fail during API call, it fails while
                        executing my code. But it's caused by an undeferabel return value from the
                        API. have you had a look at my example?

                        Comment

                        • Larry Brasfield

                          #13
                          Re: verifying valid Pointer

                          "andreas" <andreas_neu@gm x.de> wrote in message news:41ed80b2$0 $25931$9b622d9e @news.freenet.d e...[color=blue][color=green]
                          >> You are confusing "fails during API call" with
                          >> "bug is in API". Most likely, a bug in your code is causing
                          >> the problem; it just becomes apparent when that call occurs.
                          >>[/color]
                          > No, I'm not. The program does not fail during API call, it fails while
                          > executing my code. But it's caused by an undeferabel return value from the
                          > API. have you had a look at my example?[/color]

                          I see nothing posted here under your name which
                          should be expected to exhibit defined behavior.
                          Can you post a short but complete example of a
                          program which fails and which you believe should
                          behave in a way defined by the C++ standard and
                          the docs for whatever API calls you make?

                          --
                          --Larry Brasfield
                          email: donotspam_larry _brasfield@hotm ail.com
                          Above views may belong only to me.


                          Comment

                          • Victor Bazarov

                            #14
                            Re: verifying valid Pointer

                            andreas wrote:[color=blue][color=green]
                            >>You are confusing "fails during API call" with
                            >>"bug is in API". Most likely, a bug in your code is causing
                            >>the problem; it just becomes apparent when that call occurs.
                            >>[/color]
                            >
                            > No, I'm not. The program does not fail during API call, it fails while
                            > executing my code. But it's caused by an undeferabel return value from the
                            > API. have you had a look at my example?[/color]

                            If you have a problem with Windows API (or you think you have a problem
                            with it), post to comp.os.ms-windows.program mer.win32. They discuss the
                            API and how to call the functions correctly. They can help with proper
                            use of the return value. So far all you said about that API function is
                            pure speculation (no offense intended).

                            Considering for a moment that you are right, and the function is buggy and
                            it does return an invalid pointer, and the documentation doesn't explain
                            why that is, we still cannot help you -- there is no way in C++ language
                            to verify that a pointer is invalid. In that [quite unlikely] case your
                            only option is a work-around. Again, to find out what work-around to use,
                            post to the Windows newsgroup.

                            V

                            Comment

                            • Victor Bazarov

                              #15
                              Re: verifying valid Pointer

                              Larry Brasfield wrote:[color=blue]
                              > [..]
                              > Can you post a short but complete example of a
                              > program which fails and which you believe should
                              > behave in a way defined by the C++ standard and
                              > the docs for whatever API calls you make?[/color]

                              Please no API discussions here. There are other newsgroups for that.

                              Comment

                              Working...