1758028 Members
2018 Online
108866 Solutions
New Discussion юеВ

Re: SIGABRT in getenv

 
Thomas Prause
Occasional Advisor

SIGABRT in getenv

My application receives a SIGABRT in 'getenv()' when checking a environment variable, which is not set. Problem goes away if:
1. The variable is set. (no matter to what value)
2. I do not use a feature of my application which does fork() and execlp() (among some other things).

here is the stacktrace...
gdb) bt
#0 0xc0112100 in _kill () from /usr/lib/libc.2
#1 0xc00af6dc in raise () from /usr/lib/libc.2
#2 0xc00efd5c in _abort_C () from /usr/lib/libc.2
#3 0xc00efdb4 in abort () from /usr/lib/libc.2
#4 0x4ba9f8 in ?? ()
#5
#6 0xc0095fc4 in getenv () from /usr/lib/libc.2
#7 0xe9030 in main (argc=8, argv=0x7f7e0548, No.Identifier=0x7f7e056c) at backint.cpp:373
(gdb) i thr
* 1 system thread 2497 0xc0112100 in _kill () from /usr/lib/libc.2
(gdb)

This is the last action (of quite a lot of ;-) of this program. All other threads have finished. Prior calls to 'getenv()' (e.g. during startup) have succeeded. Tried to iterate through 'environ' - gives the same error. It looks like 'environ' is not correctly terminated (no zero length entry at the end). Don't know if this is a bug in HP-UX or my application. This all is on HP-UX 11.00 32-bit.

Thomas Prause
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor

Re: SIGABRT in getenv

My first thought is that you have done a putenv() using an auto declared variable that has gone out of scope.

If it ain't broke, I can fix that.
Thomas Prause
Occasional Advisor

Re: SIGABRT in getenv

Close, but ...

I call 'putenv()' two times. I changed these calls to allocate memory like:
char *my_env = new char[strlen(my_var) +1];
strcpy(my_env, my_var);
putenv(my_env);
'my_env' is never released - this should result in a memleak. But still no success. Removed my signal handler to see the orignal signal is a SIGSEGV in 'getenv()'. Used the debugger to step through 'environ' from the core: After the last valid env. vars. there are TWO invalid pointers before the end of 'environ' is marked w/ a null pointer. It looks like the allocated memory has been freed by someone else. Perplexed ...

Thomas
Thomas Prause
Occasional Advisor

Re: SIGABRT in getenv

OK, it has been solved:

Another part of the application has overwritten the env. vars, w/ identical content (quite difficult to find). But when this part finished, the memory for the vars. went out of scope.

Thanks.