Solved

getenv crashes

Posted on 2003-10-21
8
418 Views
Last Modified: 2012-06-27
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!
0
Comment
Question by:Jkrish
  • 4
  • 3
8 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 40 total points
ID: 9592253

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
 
LVL 22

Expert Comment

by:grg99
ID: 9592894
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
 
LVL 45

Expert Comment

by:Kdo
ID: 9593077

Hi grg99,

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

Kent
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Jkrish
ID: 9599060
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
 
LVL 45

Expert Comment

by:Kdo
ID: 9599763

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
 

Author Comment

by:Jkrish
ID: 9602886
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
 
LVL 45

Expert Comment

by:Kdo
ID: 9605756

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

Kent
0
 

Author Comment

by:Jkrish
ID: 9606013
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

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question