• C

getenv crashes

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?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:

getenv() has been around a long time so I doubt that the problem is with this particular function.

How about "proving" if the function's failure is its own doing, or caused by something else clobbering part of the program by running the following:

main (int argc, char **argv)
  char *Var;
  int    idx;

  for (idx = 1; idx < argc; idx++)
    Var = getenv (argv[idx]);
    printf (" %s = %s\n", argv[idx], Var == NULL ? "<none>" : Var);

Test it by doing something like this:

runit path
runit path terminal test nothing etc



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

Kent OlsenDBACommented:

Hi grg99,

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

Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

JkrishAuthor Commented:
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!
Kent OlsenDBACommented:

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.

JkrishAuthor Commented:
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");
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
Kent OlsenDBACommented:

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

JkrishAuthor Commented:

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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.