[英]secure_getenv function not giving the expected result
In my program I am using secure_getenv function to fetch some environment variables.在我的程序中,我使用 secure_getenv function 来获取一些环境变量。 I included stdlib.h in my program.
我在我的程序中包含了 stdlib.h。 This is the sample call to secure_getenv.
这是对 secure_getenv 的示例调用。
Line 1: char *myenv;第 1 行:char *myenv;
Line 2: myenv = __secure_getenv("DATA");第 2 行:myenv = __secure_getenv("DATA");
After the above lines execution, myenv points to some junk.上面几行执行后,myenv 指向一些垃圾。
I tried this within gdb after line 2.我在第 2 行之后的 gdb 内尝试了这个。
p __secure_getenv("DATA") p __secure_getenv("数据")
This prints me the DATA I wanted.这会打印出我想要的数据。
But when I try, " p myenv ", It prints the below.但是当我尝试“ p myenv ”时,它会打印以下内容。
$2 = 0×fffffffffffffe13f<Address 0xfffffffffffffe13f out of bounds>" $2 = 0×ffffffffffffffe13f<地址 0xfffffffffffffe13f 越界>”
Can the experts help me to understand what is missing & how to make this work.专家们能否帮助我了解缺少的内容以及如何进行这项工作。
Edited to add: How the myenv is actually used?编辑添加:实际如何使用myenv ? In somepoint in time my code tries to call the below.
在某个时间点,我的代码尝试调用下面的代码。
strlen(myenv) ; strlen(myenv) ;
On strlen function call, my code terminates with sig11(SIGSEGV)在 strlen function 调用中,我的代码以 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
.最可能的原因是您没有
__secure_getenv
的原型,这意味着编译器假定此 function 返回一个int
。 That int
is then cast to char*
, which sign-extends it to produce "garbage" pointer 0xfffffffffffffe13f
.然后将该
int
转换为char*
,它对其进行符号扩展以产生“垃圾”指针0xfffffffffffffe13f
。
-Wall -Wextra
-- the compiler will then warn you about the bug.-Wall -Wextra
编译您的源代码——然后编译器会警告您有关该错误的信息。#include <stdlib.h>
and use secure_getenv()
instead of __secure_getenv()
-- the former will have a proper prototype.#include <stdlib.h>
并使用secure_getenv()
而不是__secure_getenv()
——前者将有一个合适的原型。p __secure_getenv("DATA")
-- it will print the data you expect, and also the pointer value.p __secure_getenv("DATA")
中的 output——它将打印您期望的数据以及指针值。 If my guess is correct, the pointer value will have different high-order bits, but same low 32-bits as myenv == 0xfffffffffffffe13f
myenv == 0xfffffffffffffe13f
相同的低 32 位
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.