Link to home
Start Free TrialLog in
Avatar of Jkrish
Jkrish

asked on

getenv crashes

Pro*C
getenv function in User exit crashes when an environment variable is not exported

if ((envdbg = getenv("somevar")) != NULL )

any idea of why this is happening?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of grg99
grg99

Perhaps you've somehow scrozzled your local copy of the environment.

Try doing this crashy line first thing in your program. If it works there but doesnt later on,
yo may have stepped on the environment.


Hi grg99,

That's where my test code was going.  If it passes, then his program is scribbling where it aughtn't.

Kent
Avatar of Jkrish

ASKER

Problem is, the function is not a main function and it's an user exit
and it's more confusing because of the fact that other user exits (.pc files) are working fine.  
getenv is crashing even before it returns anything at all when the env variable is not exported.  

Thanks a lot guys!

Hi Jkrish,

I'm not sure what your last comment meant.


Did you try the sample code that I provided to see if getenv() has a problem.  (I still don't think that getenv() is failing on its own.)

If you've got some really good debug tools (like Rational) you should be able to discover where your program is overflowing a buffer.

If not, link the program and generate a loadmap.  Find out what's loaded right above getenv().  Chances are that a buffer one or two items above getenv() is the culprit.


Kent
Avatar of Jkrish

ASKER

Many thanks Kent!

I am calling a user exit (Pro*C) from oracle forms (version 6)
and this piece of function is in a library archive and it's a .pc file.

It's interesting to see if I open a text file and insert a line of text
and close prior to running the above mentioned command,
( fp = fopen("testfile","w");
fprintf(fp,"testline\n");
fclose(fp);
)
everything works fine.  

Like you have pointed out, an item above the call to getenv must be a culprit. But this is the first line of code in the program (only oracle variables and host variables are declared above it).  

Thanks again

I wonder if the Oracle library includes its own getenv() function?

Kent
Avatar of Jkrish

ASKER

Resolved!

It was because of the following

FILE *fp;


if ((envdbg = getenv("somevar")) != NULL )
// open a file pointer fp if somevar exists
...
...
...

if (fp)
  fprintf(fp,"print message");  <-- it's crashing here because of this
as fp was not initialized.  

FILE *fp = NULL;  // solved the problem

Thanks a lot Kent!