我正在尝试比较两个名称字符串以返回匹配的结构。我已经使用gdb验证了传递给strcmp()的两个参数对于GDB而言都不为空。但是,当我打strcmp()调用时,程序出现了段错误。
valgrind的输出是:
==5563== Invalid read of size 1
==5563== at 0x483EED7: strcmp (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==5563== by 0x10AF1B: cecs_component (cecs_component.c:74)
segfault之后的gdb输出为:
Program received signal SIGSEGV, Segmentation fault.
__strcmp_avx2 () at ../sysdeps/x86_64/multiarch/strcmp-avx2.S:102
102 ../sysdeps/x86_64/multiarch/strcmp-avx2.S: No such file or directory.
两者都使我相信我已经将NULL值传递给了strcmp(),但是如果我使用GDB来检查我在segfault之前立即传递的参数的值,它们看起来都是有效的:
Breakpoint 1, cecs_component (cecs=0x55555555f100, name=0x55555555a5c8 "position") at src/cecs_component.c:74
74 if(strcmp(name, cecs->components[i].name == 0)){
(gdb) p cecs->components[i]
$1 = {data = 0x55555555fdd0, size = 12, key = 128, name = 0x55555555a5c8 "position"}
有问题的代码段如下:
for(int i = 0; i < cecs->num_components; ++i){
if(strcmp(name, cecs->components[i].name == 0)){
return &cecs->components[i];
}
}
return NULL;
该代码段是从libcheck单元测试运行的,但是segfault会在&且CK_FORK环境变量未设置为'no'的情况下发生,因此我不认为该错误特定于libcheck。
任何帮助将不胜感激
有错字
if(strcmp(name, cecs->components[i].name == 0)){
我想你是说
if(strcmp(name, cecs->components[i].name ) == 0){