verifying valid Pointer

A

andreas

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
 
A

andreas

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

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
 
M

Mike Wahler

andreas said:
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

Change this to:

return null;

}

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?

See above, and read up about arrays and pointers.

-Mike
 
A

Andrew Koenig

How can I test, if I can access the String whithout a programm crash? A
try
catch block does not work.

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

andreas

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

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.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

andreas said:
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.

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).
 
V

Victor Bazarov

andreas said:
Andrew Koenig wrote:




Ok. there is no function in ANSI/ISO c/c++ to test the pointer. But are you
shure that there is now other way?

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

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

Ioannis Vranos

andreas said:
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?


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.
 
L

Larry Brasfield

andreas said:
Ok. there is no function in ANSI/ISO c/c++ to test the pointer. But are you
shure that there is now other way?

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.
I can't find the bug myself, cause it's in a Windows API call.

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.
 
A

andreas

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.
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?
 
L

Larry Brasfield

andreas said:
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?

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?
 
V

Victor Bazarov

andreas said:
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?

If you have a problem with Windows API (or you think you have a problem
with it), post to comp.os.ms-windows.programmer.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
 
V

Victor Bazarov

Larry said:
[..]
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?

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

Larry Brasfield

Victor Bazarov said:
Larry said:
[..]
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?

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

It is too early to diagnose the OP's problem as an
API issue. He has already posted some code which
makes clear that his grasp of C++ is shaky. You are
getting a little too zealous about topicality here. Your
calls on that are usually on the mark, but not now.

If a short program shows that the real issue is how a
particular API works, we can redirect at that time.
Until then, treating it as an API issue is just as likely
to be a waste of time in comp.os.whatever as here.
 
R

Richard Herring

Larry Brasfield said:
Victor Bazarov said:
Larry said:
[..]
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?

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

It is too early to diagnose the OP's problem as an
API issue. He has already posted some code which
makes clear that his grasp of C++ is shaky.

I thought his grasp of C++ was fine. His English may have misled you,
however. The "shaky" C++ was an example _deliberately designed_ to
simulate the effect of a buggy library function returning a pointer
which was invalid but non-null.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top