0

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; 
    }
}
  • This is the second question of this nature only today, see https://stackoverflow.com/questions/74726773/the-use-of-strcat-in-c-overwrites-irrelevant-strings. **C has no string class**, it has no magic automatic memory management. You need to study arrays then pointers then strings, in that order, without taking shortcuts. If you studied arrays and pointers then you would know why strcpy doesn't make sense here. – Lundin Dec 08 '22 at 14:52
  • a points to a static array with two const chars " " and "\0". Your strcpy writes over two constant chars (undefined behaviour) and then overwrites random data following that static array. You'll have to use malloc, or strdup may be available. The "void*" is totally irrelevant. – gnasher729 Dec 08 '22 at 14:56
  • `char *a =malloc(strlen(item1)*sizeof(char*));` over allocates. Better as `char *a = malloc(strlen(item1) +1);` – chux - Reinstate Monica Dec 08 '22 at 15:34
  • After `char *a= " ";`, `a` contains the address of a string literal. Attempting to write any data there is a mistake and the behavior will be undefined. – William Pursell Dec 08 '22 at 16:05

0 Answers0