Reputation: 73
I'm trying to compare two name strings to return a matching struct. I've verified using gdb that both parameters passed to strcmp() are non-null with GDB. However, the program segfaults when I hit the strcmp() call.
The output from valgrind is:
==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)
and the output from gdb after the segfault is:
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.
Both of these would lead me to believe that I've passed a NULL value to strcmp(), but if I use GDB to inspect the values of the parameters I'm passing immediately before the segfault they all look to be valid:
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"}
The offending snippet is below:
for(int i = 0; i < cecs->num_components; ++i){
if(strcmp(name, cecs->components[i].name == 0)){
return &cecs->components[i];
}
}
return NULL;
The snippet is being run from a libcheck unit test, but the segfault occurs with & without the CK_FORK environment variable set to 'no', so I don't believe the error is libcheck specific.
Any help would be greatly appreciated
Upvotes: 2
Views: 949
Reputation: 311068
There is a typo
if(strcmp(name, cecs->components[i].name == 0)){
I think you mean
if(strcmp(name, cecs->components[i].name ) == 0){
Upvotes: 1