In my program I am using secure_getenv function to fetch some environment variables. I included stdlib.h in my program. This is the sample call to secure_getenv.
Line 1 : char *myenv;
Line 2 : myenv = __secure_getenv("DATA");
After the above lines execution, myenv points to some junk.
I tried this within gdb after line 2.
p __secure_getenv("DATA")
This prints me the DATA I wanted.
But when I try, "p myenv", It prints the below.
$2 = 0×fffffffffffffe13f<Address 0xfffffffffffffe13f out of bounds>"
Can the experts help me to understand what is missing & how to make this work.
Edited to add: How the myenv is actually used? In somepoint in time my code tries to call the below.
strlen(myenv);
On strlen function call, my code terminates with sig11(SIGSEGV)
The most likely cause is that you don't have a prototype for
__secure_getenv, which means that the compiler assumes that this function returns anint. Thatintis then cast tochar*, which sign-extends it to produce "garbage" pointer0xfffffffffffffe13f.-Wall -Wextra-- the compiler will then warn you about the bug.#include <stdlib.h>and usesecure_getenv()instead of__secure_getenv()-- the former will have a proper prototype.p __secure_getenv("DATA")-- it will print the data you expect, and also the pointer value. If my guess is correct, the pointer value will have different high-order bits, but same low 32-bits asmyenv == 0xfffffffffffffe13f