Hi, I have some memory issues, somehow I am freeing already freed memory but I really cant find it after staring at it for quite long while, valgrind is writing sth like this... btw program works how it should but i have to deal with those 16 bytes...
I would be gratefull for any help. thanks
Code:
==3200== Invalid write of size 8==3200== at 0x400B88: deleteList (in /home/xnovak11/Downloads/C/hwDirectories/main)
==3200== by 0x400D4C: mainFunc (in /home/xnovak11/Downloads/C/hwDirectories/main)
==3200== by 0x400E87: main (in /home/xnovak11/Downloads/C/hwDirectories/main)
==3200== Address 0x51d4840 is 0 bytes inside a block of size 16 free'd
==3200== at 0x4C2695D: free (vg_replace_malloc.c:366)
==3200== by 0x400B74: deleteList (in /home/xnovak11/Downloads/C/hwDirectories/main)
==3200== by 0x400D4C: mainFunc (in /home/xnovak11/Downloads/C/hwDirectories/main)
==3200== by 0x400E87: main (in /home/xnovak11/Downloads/C/hwDirectories/main)
==3200==
==3200==
==3200== HEAP SUMMARY:
==3200== in use at exit: 0 bytes in 0 blocks
==3200== total heap usage: 35 allocs, 35 frees, 165,099 bytes allocated
==3200==
==3200== All heap blocks were freed -- no leaks are possible
==3200==
==3200== For counts of detected and suppressed errors, rerun with: -v
==3200== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "correctperms.h"
int corrected=0;
int isDir(const char* name){
char* pName = malloc (strlen(name)+1 * sizeof(char));
strcpy(pName,name);
char* help = pName + strlen(name)-3;
if (strlen(pName)>126) {
free(pName);
fprintf(stderr,"dlha cesta\n");
return 2;
}
if (strchr(help,'.')!=NULL){
free(pName);
return 2;
}
DIR* p_look_dir= opendir(pName);
free(pName);
if (p_look_dir!=NULL ){
closedir(p_look_dir);
return 1;
}
return 0;
}
listItem* prev;
listItem* cur;
listItem* list = NULL;
int prechod;
void add(const char* name, listItem** list){
listItem *newItem = malloc(sizeof(listItem));
newItem->tableName = malloc(strlen(name)+1*sizeof(char));
strcpy(newItem->tableName,name);
newItem->next = *list;
*list = newItem;
}
void scan(listItem** list){
prev = *list;
if (prev->next==NULL) {
cur = prev;
prechod=0;
}else {
cur = prev->next;
prechod=1;
}
if (prev!=NULL){
while (cur->next != NULL){
prev=cur;
cur= cur->next;
}
}
if (*list == NULL ){
cur= NULL;
prev= NULL;
}
}
void deleteList (){
if(cur->tableName!=NULL)
free(cur->tableName);
if (cur)
free(cur);
if (prev!=NULL)
prev->next =NULL;
}
int mainFunc(const char* buffer){
add(buffer,&list);
DIR *directory = NULL;
struct stat sb;
struct dirent *pFile = NULL;
do{
scan(&list);
directory = opendir(cur->tableName);
pFile=readdir(directory);
while (pFile!=NULL){
char* predlzCestuODir = malloc(strlen(pFile->d_name)+ strlen(cur->tableName)+2 * sizeof(char));
strcpy(predlzCestuODir,cur->tableName);
strcat(predlzCestuODir,pFile->d_name);
int flag = isDir(predlzCestuODir);
if (flag==1){
strcat(predlzCestuODir,"/");
add(predlzCestuODir,&list);
}
if (flag==0){
if (stat(predlzCestuODir, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
if (sb.st_mode & S_IROTH){
corrected++;
printf("%s\n",predlzCestuODir);
}
printf("%s",predlzCestuODir);
}
free(predlzCestuODir);
pFile=readdir(directory);
}
closedir(directory);
scan(&list);
deleteList ();
}while(prechod==1);
return corrected;
}
Code:
#ifndef CORRECTPERMS_H_INCLUDED#define CORRECTPERMS_H_INCLUDED
typedef struct listItem{
struct listItem* next;
char* tableName;
} listItem;
void deleteList ();
void scan(listItem** list);
void add(const char* name, listItem** list);
int isDir(const char* name);
int mainFunc(const char* buffer);
#endif // CORRECTPERMS_H_INCLUDED
Code:
#include <stdio.h>#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "correctperms.h"
int main(int argc,char *argv[])
{
// if (argc!=1){
// fprintf(stderr,"error: could not open file\n");
// return -1;
// }
int i;
char* lomitko = malloc (strlen(argv[1])+1 * sizeof(char));
strcpy (lomitko,argv[1]);
char* help = lomitko +strlen(lomitko)-1;
if (*help=='/'){
free(lomitko);
i=mainFunc(argv[1]);
printf("Corrected %d files.\n",i);
}else{
free(lomitko);
lomitko = malloc (strlen(argv[1])+2 * sizeof(char));
strcpy(lomitko,argv[1]);
strcat (lomitko,"/");
i=mainFunc(lomitko);
free(lomitko);
printf("Corrected %d files.\n",i);
}
return 0;
}