sisoputnfrba / foro

Foro de consultas para el trabajo práctico
148 stars 7 forks source link

Hacer un free( ) de un puntero me rompe todo #1959

Closed julchat closed 3 years ago

julchat commented 3 years ago

Hola, estuve queriendo hacer pruebas por la consola de cliente, y para primero probar al cliente como si, lo debugee. La siguiente función me trajo problemas

void procesarEntrada(mensajeListoYSeparado* paraMandar){
    t_message* paquete = malloc(sizeof(t_message));
    paquete->head = *paraMandar->operacion;
    paquete->size += sizeof(t_header);
    void* aEnviar = malloc(sizeof(size_t) + sizeof(t_header));
    int indicePedido;
    switch(*paraMandar->operacion){
    case GUARDAR_PLATO: ;
        t_guardarPlato guardarPlato;
        guardarPlato.idPedido = atoi(list_get(paraMandar->parametros,0));
        paquete->size += sizeof(uint32_t);
        guardarPlato.cantidad  = atoi(list_get(paraMandar->parametros,1));
        paquete->size += sizeof(uint32_t);
        guardarPlato.nombreRestaurante = list_get(paraMandar->parametros,2);
        paquete->size += strlen(guardarPlato.nombreRestaurante)+1;
        guardarPlato.platoParaAgregar = list_get(paraMandar->parametros,3);
        paquete->size += strlen(guardarPlato.platoParaAgregar)+1;
        paquete->content = serializarGuardarPlato(guardarPlato);
        paquete->size += 2* sizeof(uint32_t); //tamanios de los strings
        break;
    }
    int offset2 = 0;
    memcpy(aEnviar + offset2, &paquete->size, sizeof(size_t));
        offset2 += sizeof(size_t);
    memcpy(aEnviar+ offset2, &paquete->head, sizeof(t_header));
    offset2+= sizeof(t_header);
    memcpy(aEnviar+ offset2, paquete->content, paquete->size - sizeof(t_header));
    offset2 += paquete->size - sizeof(t_header);
    send(socketDeHabla, aEnviar, paquete->size + sizeof(size_t),0);
    free(paquete->content);
    ... Esto sigue pero no llegá a más

Con la función de serializar siendo

void* serializarGuardarPlato(t_guardarPlato info){
    void* content = malloc(sizeof(uint32_t) + strlen(info.nombreRestaurante)+1 + sizeof(uint32_t) + sizeof(uint32_t) + strlen(info.platoParaAgregar)+1 + sizeof(uint32_t));
    int offset = 0;
    uint32_t tamanioRestaurante = strlen(info.nombreRestaurante)+1;
    uint32_t tamanioPlato = strlen(info.platoParaAgregar)+1;
    memcpy(content, &info.idPedido, sizeof(uint32_t));
    offset+= sizeof(uint32_t);
    memcpy(content, &info.cantidad, sizeof(uint32_t));
    offset+= sizeof(uint32_t);
    memcpy(content, &tamanioRestaurante, sizeof(uint32_t));
    offset+= sizeof(uint32_t);
    memcpy(content, info.nombreRestaurante, tamanioRestaurante);
    offset+= tamanioRestaurante;
    memcpy(content, &tamanioPlato, sizeof(uint32_t));
    offset+= sizeof(uint32_t);
    memcpy(content, info.platoParaAgregar, tamanioPlato);
    offset+= tamanioPlato;
    return content;
}

Cuando yo le hago free a (paquete->content), al que lo igualé cuando entré en el switch (entró, lo vi en el debugeo) e use la función de serializar que me devuelve un void* malloqueado, me rompe todo y me tira esto.

>*** Error in `/home/utnso/tp-2020-2c-PoderRecursante/Cliente/Debug/Cliente': double free or corruption (out): 0xb5f00490 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x67377)[0xb7dfc377]
/lib/i386-linux-gnu/libc.so.6(+0x6d2f7)[0xb7e022f7]
/lib/i386-linux-gnu/libc.so.6(+0x6dc31)[0xb7e02c31]
/home/utnso/tp-2020-2c-PoderRecursante/Cliente/Debug/Cliente[0x804a7e7]
/lib/i386-linux-gnu/libpthread.so.0(+0x6295)[0xb7f52295]
/lib/i386-linux-gnu/libc.so.6(clone+0x6e)[0xb7e7c0ae]
======= Memory map: ========
08048000-0804e000 r-xp 00000000 fc:00 1442510    /home/utnso/tp-2020-2c-PoderRecursante/Cliente/Debug/Cliente
0804e000-0804f000 r-xp 00005000 fc:00 1442510    /home/utnso/tp-2020-2c-PoderRecursante/Cliente/Debug/Cliente
0804f000-08050000 rwxp 00006000 fc:00 1442510    /home/utnso/tp-2020-2c-PoderRecursante/Cliente/Debug/Cliente
08050000-08071000 rwxp 00000000 00:00 0          [heap]
b5f00000-b5f21000 rwxp 00000000 00:00 0 
b5f21000-b6000000 ---p 00000000 00:00 0 
b60ce000-b60cf000 ---p 00000000 00:00 0 
b60cf000-b68cf000 rwxp 00000000 00:00 0 
b68cf000-b6a00000 r-xp 00197000 fc:00 1833800    /usr/lib/locale/locale-archive
b6a00000-b6c00000 r-xp 00000000 fc:00 1833800    /usr/lib/locale/locale-archive
b6c00000-b6c21000 rwxp 00000000 00:00 0 
b6c21000-b6d00000 ---p 00000000 00:00 0 
b6d3a000-b6d56000 r-xp 00000000 fc:00 1700661    /lib/i386-linux-gnu/libgcc_s.so.1
b6d56000-b6d57000 rwxp 0001b000 fc:00 1700661    /lib/i386-linux-gnu/libgcc_s.so.1
b6d6f000-b6d70000 ---p 00000000 00:00 0 
b6d70000-b7570000 rwxp 00000000 00:00 0 
b7570000-b7571000 ---p 00000000 00:00 0 
b7571000-b7d72000 rwxp 00000000 00:00 0 
b7d72000-b7d92000 r-xp 00000000 fc:00 1700744    /lib/i386-linux-gnu/libtinfo.so.5.9
b7d92000-b7d94000 r-xp 0001f000 fc:00 1700744    /lib/i386-linux-gnu/libtinfo.so.5.9
b7d94000-b7d95000 rwxp 00021000 fc:00 1700744    /lib/i386-linux-gnu/libtinfo.so.5.9
b7d95000-b7f45000 r-xp 00000000 fc:00 1705948    /lib/i386-linux-gnu/libc-2.23.so
b7f45000-b7f47000 r-xp 001af000 fc:00 1705948    /lib/i386-linux-gnu/libc-2.23.so
b7f47000-b7f48000 rwxp 001b1000 fc:00 1705948    /lib/i386-linux-gnu/libc-2.23.so
b7f48000-b7f4c000 rwxp 00000000 00:00 0 
b7f4c000-b7f65000 r-xp 00000000 fc:00 1705947    /lib/i386-linux-gnu/libpthread-2.23.so
b7f65000-b7f66000 r-xp 00018000 fc:00 1705947    /lib/i386-linux-gnu/libpthread-2.23.so
b7f66000-b7f67000 rwxp 00019000 fc:00 1705947    /lib/i386-linux-gnu/libpthread-2.23.so
b7f67000-b7f69000 rwxp 00000000 00:00 0 
b7f69000-b7fa7000 r-xp 00000000 fc:00 1700723    /lib/i386-linux-gnu/libreadline.so.6.3
b7fa7000-b7fa8000 ---p 0003e000 fc:00 1700723    /lib/i386-linux-gnu/libreadline.so.6.3
b7fa8000-b7fa9000 r-xp 0003e000 fc:00 1700723    /lib/i386-linux-gnu/libreadline.so.6.3
b7fa9000-b7fac000 rwxp 0003f000 fc:00 1700723    /lib/i386-linux-gnu/libreadline.so.6.3
b7fac000-b7fad000 rwxp 00000000 00:00 0 
b7fad000-b7fb2000 r-xp 00000000 fc:00 1441104    /home/utnso/tp-2020-2c-PoderRecursante/Libreria/Debug/libLibreria.so
b7fb2000-b7fb3000 r-xp 00004000 fc:00 1441104    /home/utnso/tp-2020-2c-PoderRecursante/Libreria/Debug/libLibreria.so
b7fb3000-b7fb4000 rwxp 00005000 fc:00 1441104    /home/utnso/tp-2020-2c-PoderRecursante/Libreria/Debug/libLibreria.so
b7fb4000-b7fbc000 r-xp 00000000 fc:00 1848626    /usr/lib/libcommons.so
b7fbc000-b7fbd000 r-xp 00007000 fc:00 1848626    /usr/lib/libcommons.so
b7fbd000-b7fbe000 rwxp 00008000 fc:00 1848626    /usr/lib/libcommons.so
b7fce000-b7fcf000 rwxp 00000000 00:00 0 
b7fcf000-b7fd6000 r-xs 00000000 fc:00 1832176    /usr/lib/i386-linux-gnu/gconv/gconv-modules.cache
b7fd6000-b7fd7000 rwxp 00000000 00:00 0 
b7fd7000-b7fda000 r--p 00000000 00:00 0          [vvar]
b7fda000-b7fdb000 r-xp 00000000 00:00 0          [vdso]
b7fdb000-b7ffe000 r-xp 00000000 fc:00 1705946    /lib/i386-linux-gnu/ld-2.23.so
b7ffe000-b7fff000 r-xp 00022000 fc:00 1705946    /lib/i386-linux-gnu/ld-2.23.so
b7fff000-b8000000 rwxp 00023000 fc:00 1705946    /lib/i386-linux-gnu/ld-2.23.so
bffdf000-c0000000 rwxp 00000000 00:00 0          [stack]
mesaglio commented 3 years ago

Buenas Cris, primero que nada te menciono que de nuestro lado se nos hace mucho mas rapido leer codigo formateado. Esto lo podes hacer con esto.

Respecto a tu error, dice doble free o corruption. El doble free, significa que al momento que queres liberar esa memoria, alguien lo hizo antes.

Como primera recomendacion, te diria que lo corras con valgrind para analizarlo mejor. Algo que vi en la función de serialización, es que al hacer los memcpy sobre content pero no te vas desplazando el offset.

Si queres podes compartir el log del valgrind (con formato Markdown).

Pd: podes ejecutar valgrind --log-file="filename" para llevar todo el output de valgrind a un archivo. Pd2: Como usar valgrind?

karengrams commented 3 years ago

¡Buenas buenas!

¿Como va? ¿Pudiste solucionar el problema? 😺

¡Saludos!

karengrams commented 3 years ago

Cierro por inactividad, pero cualquier cosa, no dudes en abrirlo 😄 .