I've got a
*** glibc detected *** [...] free(): invalid pointer: 0x0804d0d0 ***
in this function:
ptrGBloque determinar_nodo(const char* path){
// Si es el directorio raiz, devuelve 0:
if(!strcmp(path, "/")) return 0;
int fd, i, nodo_anterior, aux;
// Super_path usado para obtener la parte superior del path, sin el nombre.
char *super_path = (char*) malloc(strlen(path)), *nombre = (char*) malloc(strlen(path));
char *start = nombre, *start_super_path = super_path; //Estos liberaran memoria.
struct grasa_file_t *node, *inicio;
unsigned char *node_name;
strcpy(super_path, path);
strcpy(nombre, path);
// Obtiene y acomoda el nombre del archivo.
if (lastchar(path, '/')) {
nombre[strlen(nombre)-1] = '\0';
}
nombre = strrchr(nombre, '/');
nombre[0] = '\0';
nombre = &nombre[1]; // Acomoda el nombre, ya que el primer digito siempre es '/'
// Acomoda el super_path
if (lastchar(super_path, '/')) {
super_path[strlen(super_path)-1] = '\0';
}
aux = strlen(super_path) - strlen(nombre);
super_path[aux] = '\0';
nodo_anterior = determinar_nodo(super_path);
// Abrir conexion y traer directorios, guarda el bloque de inicio para luego liberar memoria
if ((fd = open(DISC_PATH, O_RDONLY, 0)) == -1) {
printf("ERROR");
return -ENOENT;
}
node = (void*) mmap(NULL, HEADER_SIZE_B + BITMAP_SIZE_B + NODE_TABLE_SIZE_B , PROT_READ, MAP_SHARED, fd, 0);
inicio = node;
node = &(node[GFILEBYBLOCK + BITMAP_BLOCK_SIZE]);
// Busca el nodo sobre el cual se encuentre el nombre.
node_name = &(node->fname[0]);
for (i = 0; ( (node->parent_dir_block != nodo_anterior) | (strcmp(nombre, (char*) node_name) != 0) | (node->state == 0)) & (i < GFILEBYTABLE) ; i++ ){
node = &(node[1]);
node_name = &(node->fname[0]);
}
// Cierra conexiones y libera memoria.
free(start);
free(start_super_path);
if (munmap(inicio, HEADER_SIZE_B + BITMAP_SIZE_B + NODE_TABLE_SIZE_B) == -1) printf("ERROR");
close(fd);
if (i >= GFILEBYTABLE) return -1;
return (i+1);
}
The problem happens when I call this function with:
determinar_nodo("/Otra Carpetita/Inside Otra/Inside Otra Otra :D/");
There are many redundant steps in the code because of debbuging, just try to step them over.
Which really matters to me is the memory problem. Notice that at the beggining of the declarations I create the start and start_super_path pointers just not to loose the address to free at the end of the function.
In addition, it seems to be that the function breaks at the second call if it. It means that the first "free" are working properly, which correspond to the call of determinar_nodo("/Otra Carpetita/")
, but it breaks at the free of the call determinar_nodo("/Otra Carpetita/Inside Otra/")
.
Debbuging on the memory browser, I could notice that the adresses that the function is trying to free are indeed correct, so I don't really know what could be going on. Anyway, if I free any of those, I got the mentioned error.
Thanks in advance for your help, and please forgive my poor english.