Seg fault when accessing double pointer

C

cerr

Hi There,

I wrote a function that reads out all the files in a certain directory
and it's writing them into an array of character arrays that was
passed. The function looks like this:
Code:
int getdir(char *path, char **filelist)
{
int items=0;
char **temp=NULL;
char filename[1024]={0};
struct dirent *dp;
DIR *dir = opendir(path);
if (dir==NULL){
syslog(LOG_ALERT, "Error opening log_record directory %s", path);
printf("Error opening log_record directory %s", path);
return -1;
}
while((dp=readdir(dir))!=NULL){
if(strstr(dp->d_name, "log_record")!=NULL){
sprintf(filename,"%s%s",path,dp->d_name);
syslog(LOG_ALERT, "%s\n",filename);
temp = realloc(filelist, (items+1) * sizeof(*temp));
if (temp == NULL) {
while (--items >= 0)
free (filelist[items]);
free (filelist);
syslog(LOG_ALERT, "Error reallocating memory for filelist\n");
printf("Error reallocating memory for filelist\n");
return -1;
}
filelist = temp;
filelist[items] = calloc(strlen(filename)+1,sizeof(char));
if (filelist[items] == NULL) {
syslog(LOG_ALERT, "Error reallocating memory for filename\n");
printf("Error reallocating memory for filename\n");
return -1;
}
strncpy (filelist[items], filename, strlen(filename));
++items;
}
}
printf("filelist: %s\n",filelist[items-1]);
return items;
}


and i'm calling it like this:
Code:
files=getdir("/usr/share/NovaxTSP/", list);
printf("files %d - %s\n",files,list[files-1]);

where list is of typer char**
Now my problem is that my application segfaults out at the printf even
tho i know that files value is 1 and if i do the same printf inside
the function, it works just well, i do see the file path. What's going
on here?
Thank you!
Ron
 
B

Barry Schwarz

Hi There,

I wrote a function that reads out all the files in a certain directory
and it's writing them into an array of character arrays that was
passed. The function looks like this:
Code:
int getdir(char *path, char **filelist)
{
int items=0;
char **temp=NULL;
char filename[1024]={0};
struct dirent *dp;
DIR *dir = opendir(path);
if (dir==NULL){
syslog(LOG_ALERT, "Error opening log_record directory %s", path);
printf("Error opening log_record directory %s", path);
return -1;
}
while((dp=readdir(dir))!=NULL){
if(strstr(dp->d_name, "log_record")!=NULL){
sprintf(filename,"%s%s",path,dp->d_name);
syslog(LOG_ALERT, "%s\n",filename);
temp = realloc(filelist, (items+1) * sizeof(*temp));
if (temp == NULL) {
while (--items >= 0)
free (filelist[items]);
free (filelist);
syslog(LOG_ALERT, "Error reallocating memory for filelist\n");
printf("Error reallocating memory for filelist\n");
return -1;
}
filelist = temp;
filelist[items] = calloc(strlen(filename)+1,sizeof(char));

Setting the allocated memory to zeros is a waste of time since you
overwrite them immediately.
if (filelist[items] == NULL) {
syslog(LOG_ALERT, "Error reallocating memory for filename\n");
printf("Error reallocating memory for filename\n");
return -1;

Strange that you clean up your allocated memory when malloc fails
above but you don't when calloc fails here. This leads to memory
leaks.
}
strncpy (filelist[items], filename, strlen(filename));

Why go through the wasted effort of calling strlen twice when strcpy
will achieve the same result here?
++items;
}
}
printf("filelist: %s\n",filelist[items-1]);
return items;
}


and i'm calling it like this:
Code:
files=getdir("/usr/share/NovaxTSP/", list);
printf("files %d - %s\n",files,list[files-1]);

where list is of typer char**
Now my problem is that my application segfaults out at the printf even
tho i know that files value is 1 and if i do the same printf inside
the function, it works just well, i do see the file path. What's going
on here?

C passes arguments by value. realloc makes the current value of list
indeterminate if a new area is allocated (as opposed to the current
area being extended). The updated value of filelist in getdir (which
is perfectly usable within that function) is never passed back to your
calling function. Therefore, the expression list[files-1] invokes
undefined behavior.

And for what it is worth, the newly allocated area is inaccessible
when getdir returns, thus creating a memory leak.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

dynamic array of character arrays 13
seg fault 10
folder parsing (newbie)problem 5
Fibonacci 0
cbuf with char *s 22
Eliminating this seg fault 5
Error When trying to free a pointer 5
Quickly get count of files on linux 8

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top