i have this function that accept void item and it has to copy into a string and compare them...This because in future i wanto to modify the copy in upper case and i don't want that the element passed by parameter change. How i can i do it? Address Sanitaizer give me alway a different error despite i tried different way to allocate the variable.
int compare_string(void* item1, void* item2){
char *a =" ";
char *b= " ";
strcpy(a,item1);
strcpy(b,item2);
if(strcmp(a,b)==0) return 0;
else if(strcmp(a,b)>0) return 1;
else return -1;
}
==12556==ERROR: AddressSanitizer: SEGV on unknown address 0x555d336ce39d (pc 0x148b654e2168 bp 0x7ffebf70ead0 sp 0x7ffebf70eaa8 T0) ==12556==The signal is caused by a WRITE memory access. #0 0x148b654e2168 (/lib/x86_64-linux-gnu/libc.so.6+0x19f168) #1 0x555d336cd265 in compare_string (/home/matteo/Scrivania/Algo/laboratorio-algoritmi-2021-2022-main/Esercizio 2/ex2/build/main+0x2265) #2 0x555d336cd110 in search_skip_list (/home/matteo/Scrivania/Algo/laboratorio-algoritmi-2021-2022-main/Esercizio 2/ex2/build/main+0x2110) #3 0x555d336cc86c in search_in_file (/home/matteo/Scrivania/Algo/laboratorio-algoritmi-2021-2022-main/Esercizio 2/ex2/build/main+0x186c) #4 0x555d336cca1e in main (/home/matteo/Scrivania/Algo/laboratorio-algoritmi-2021-2022-main/Esercizio 2/ex2/build/main+0x1a1e) #5 0x148b6536cd8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58 #6 0x148b6536ce3f in __libc_start_main_impl ../csu/libc-start.c:392 #7 0x555d336cc304 in _start (/home/matteo/Scrivania/Algo/laboratorio-algoritmi-2021-2022-main/Esercizio 2/ex2/build/main+0x1304) AddressSanitizer can not provide additional info.
i solved in this way: (i posted cause strlen doesn't worked but now yes, i think that i forgot some poninters before and i posted the older version)
int compare_string(void* item1, void* item2){
char *a =malloc(strlen(item1)*sizeof(char*));
char *b= malloc(strlen(item2)*sizeof(char*));
strcpy(a,item1);strcpy(b,item2);
if(strcmp(a,b)==0) {
free(a);free(b);
return 0;
}
else if(strcmp(a,b)>0) {
free(a);free(b);
return 1;
}
else {
free(a);free(b);
return -1;
}
}