• C

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!
JkrishAsked:
Who is Participating?
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 OlsenData Warehouse Architect / DBACommented:

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


Kent




0

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
grg99Commented:
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.

0
Kent OlsenData Warehouse Architect / DBACommented:

Hi grg99,

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

Kent
0
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

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!
0
Kent OlsenData Warehouse Architect / DBACommented:

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
0
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");
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
0
Kent OlsenData Warehouse Architect / DBACommented:

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

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

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.