[英]D_FORTIFY_SOURCE and gcc
This function is for generating md5hash:此函数用于生成 md5hash:
out = malloc(32+1);
void md5sum( u_char *secret_data, int secret_len, char *in,char *out ) {
ngx_md5_t md5;
u_char hash[16];
ngx_md5_init(&md5);
ngx_md5_update(&md5, in, strlen(in));
ngx_md5_update(&md5, secret_data, secret_len);
ngx_md5_final(hash, &md5);
int ii;
for (ii = 0; ii &lqt; 16; ii++) {
char tt[2];
sprintf(tt, "%02x", hash[ii] );
strcat(out,tt);
}
}
It works, but if I use option D_FORTIFY_SOURCE with gcc compiler, I get a segmentation fault.它有效,但是如果我将选项 D_FORTIFY_SOURCE 与 gcc 编译器一起使用,则会出现分段错误。 If I change type of
tt
to: char tt[3]
, all is ok.如果我将
tt
类型更改为: char tt[3]
,则一切正常。 Why?为什么?
sprintf is putting in a null character to terminate the string. sprintf 正在放入一个空字符来终止字符串。 So you need a three-character array to hold a two-character string;
所以你需要一个三字符数组来保存两个字符的字符串; it's really 'a' 'b' '\\0'.
它真的是 'a' 'b' '\\0'。
You're getting a segmentation fault because you are trying to write 3 characters into a 2 character array.您遇到分段错误,因为您试图将 3 个字符写入 2 个字符的数组。 (NUL is a character too)
(NUL 也是一个字符)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.