简体   繁体   English

C crypt函数,malloc和valgrind

[英]C crypt function, malloc and valgrind

My man page for the crypt function states that: 我的crypt函数手册页指出:

"The return value points to static data whose content is overwritten by each call." “返回值指向静态数据,其每次调用都会覆盖其内容。”

However, when using the SHA512 version (ie, the salt starts $6$...), valgrind does not seem to agree. 但是,当使用SHA512版本(即salt起价$ 6 $ ...)时,valgrind似乎不同意。 Unless I free the pointer that crypt returns, it gets upset: 除非我释放crypt返回的指针,否则它将不高兴:

120 bytes in 1 blocks are still reachable in loss record 1 of 1
at 0x4C2BBA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x4C2DF4F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x521F4D4: __sha512_crypt (sha512-crypt.c:437)

Conversely, valgrind is fine if I use the DES version (so salt does not start with $6$ or similar). 相反,如果我使用DES版本,则valgrind很好(因此,salt开头不是$ 6 $或类似符号)。

What's going on here and is this behaviour explained anywhere? 这是怎么回事,此行为在任何地方都有解释吗?

Thanks in advance. 提前致谢。

EDIT: Platform is Ubuntu 15.04 64-bit. 编辑:平台是Ubuntu 15.04 64位。 Here's a program: 这是一个程序:

#define _XOPEN_SOURCE 700
#include <unistd.h>

int main(int argc, char** argv) {
    char *hash = crypt("password", "$6$Salty");
    return 0;
}

For some crypt variations, the preallocated buffer is not big enough, so it allocates (via malloc) a buffer that will be reused by the next call to crypt that needs a large buffer (possibly after realloc ing it). 对于一些隐窝变化,预先分配的缓冲区不够大,所以它分配(通过malloc的),将通过下一个调用重用的缓冲区crypt ,需要一个大的缓冲区(可能以后realloc荷兰国际集团的话)。 That's why it is noted as "still reachable" by valgrind -- there's a static variable in the library that points at the block. 这就是为什么valgrind将其标记为“仍可访问”的原因-库中有一个指向该块的静态变量。

If you were to free it, it's likely the next call to crypt would misbehave (likely giving a runtime error about reusing a freed block). 如果要释放它,则下一次对crypt的调用很可能会出现异常(可能会给出有关重新使用释放块的运行时错误)。

No matter how many times you call crypt there will be one block identified by valgrind like this. 无论您多少次调用crypt ,都会有一个这样的valgrind标识的块。 It isn't a real memory leak, just constant overhead from the library that is pretty much impossible to avoid. 这不是真正的内存泄漏,只是库中不断增加的开销,几乎是无法避免的。

Generally you want to ignore valgrind messages about "still reachable" blocks unless the amount of memory is unexpectedly large, or the requests are coming from a place that should not be storing the returned pointers in global variables. 通常,除非内存量意外大,或者请求来自不应将返回的指针存储在全局变量中的位置,否则您通常要忽略有关“仍可到达”块的valgrind消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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