简体   繁体   中英

secure_getenv function not giving the expected result

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)

Can the experts help me to understand what is missing & how to make this work.

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 an int . That int is then cast to char* , which sign-extends it to produce "garbage" pointer 0xfffffffffffffe13f .

  1. You should compile your source with -Wall -Wextra -- the compiler will then warn you about the bug.
  2. You should #include <stdlib.h> and use secure_getenv() instead of __secure_getenv() -- the former will have a proper prototype.
  3. You can compare the output from 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 as myenv == 0xfffffffffffffe13f

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM