Square Galaxy

atoi(getenv(”MYVAR”))

by Jacob on May.28, 2008, under Technical

These are mostly personal notes. I’m not sure if they will make sense to others.

I had a problem with a line of code like this:
int x = atoi(getenv("MYVAR"));

If this line was in daemon-ized code started at init level 2, it would segfault. (I’m not sure if these conditions are necessary, but those were my conditions).

getenv("UNKNOWN_VAR") == NULL
atoi(NULL) should == 0

But for some reason, things were segfaulting. I corrected the problem with:
if (getenv("MYVAR") == NULL) int x = 0;
else int x = atoi(getenv("MYVAR"));

Related posts:

  1. What bothers you most about this code?
  2. Cross post code

:c, linux, programming, Software
2 comments for this entry:
  1. alex

    That’s pretty weird. Is it only runlevel 2? If that’s the case it is probably an environment variable that depends on some higher-level initializations. In which case if you really wanted to know what it depended on you could start fooling around with your initscripts in your VM. You could probably narrow it down to a few suspicious ones and figure it out fairly quick.

  2. jacob

    I thought it might have been the getenv, but getenv(”PATH”) worked, and I verified that getenv(”UNKNOWN”) properly returned NULL. So I think its the atoi(NULL) that is the problem.

    It might not work at other runlevels as well, but I didn’t test those.

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

 

November 2008
M T W T F S S
« Oct    
 12
3456789
10111213141516
17181920212223
24252627282930