Solved

getenv Crashes!

Posted on 2004-10-20
6
1,120 Views
Last Modified: 2008-03-17
I noticed that when i use getenv to get a particular environment variable, the program crashes...

However, i wanted the environemnt variables to be optional.  So, if the env variables are not set, then use the default ones.

But when it's not defined, ...it shuts down.
0
Comment
Question by:jewee
6 Comments
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12364148
Hi jewee,
Please post the relevant part of the code so that we can go further.

Cheers!
0
 
LVL 30

Expert Comment

by:Axter
ID: 12364166
What OS and what compiler are you using?

I had a similar problem like this using GNU compiler on SunOS.
The problem was some where else in the code, but it would currupt the memory so that it would crash when calling getenv.
0
 

Author Comment

by:jewee
ID: 12364250
I call the getEnvVar method in the constructor...

bool ViewForm::getEnvVar()
{
   serverHostName = getenv("SERVER_HOSTNAME");
   if (serverHostName == NULL)
   {
        printf("Error\n");
        return false;
    }
}

I am running on a SunOS
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 12

Accepted Solution

by:
stefan73 earned 500 total points
ID: 12368002
Hi jewee,
Your code snippet looks OK. Just make sure that you don't access (dereference) serverHostName when it's NULL.

You get a core dump, right? Then you can compile your source with -g, run it until a core dump is created and then check either with Workshop or dbx:

cc -g file.c -o binary
./binary # crash!

dbx ./binary core

Cheers!

Stefan
0
 
LVL 30

Expert Comment

by:Axter
ID: 12379211
Is your constructor being called before entry into the main() function?
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12408758
serverHostName is going to point to read-only memory. You don't do anything like accidentally free/deleting it or writing to it do you?
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now