Reading file line by line with fgets in C, invalid size reading

I keep getting valgrind error in my code and after three hours I remain clueless, so I need your help people.

Therefore, I basically just read the files contained in the directory and parse them. So I copied the shortest example of my code still causing an error:

int main(int argc, char** argv) {

 parse_files_dir("/Users/link_to_dir_example/");
 return (EXIT_SUCCESS);
}

void parse_files_dir(char *dirLink){

int dLink_l =strlen(dirLink);
int max_len = dLink_l*2;

char* full_path=malloc(sizeof(char)*(max_len+1));
//check if null pointer...

strncpy(full_path, dirLink, dLink_l);

DIR *dir;
struct dirent *dir_con;
dir=opendir(dirLink);

if (dir == NULL){
    fprintf(stderr, "Problem opening directory: \"%s\". Aborting...\n", dirLink);
    exit(EXIT_FAILURE);
}

while((dir_con = readdir(dir)) != NULL){

    if (dir_con->d_name[0] == '.') continue;

    if (dLink_l+strlen(dir_con->d_name)>max_len) //realloc full path..

    strncpy(&full_path[dLink_l], dir_con->d_name, strlen(dir_con->d_name));
    full_path[dLink_l+strlen(dir_con->d_name)] = '\0';        

    parse_one_file(full_path);  // (*) <=== valgrind complain 
    full_path[dLink_l] = '\0';
} 
free(full_path);
closedir(dir);
}

So now the actual problem method:

void parse_one_file(char* link) {

FILE *file = fopen(link, "r");   
if (file == NULL) //error message

int line_len=0;
int line_max=1000;
char* line= malloc(sizeof(char)*line_max);
line[0] = '\0';

char* line_full= malloc(sizeof(char)*line_max);
line_full[0] = '\0';
int line_full_len = 0;

//check all allocations for null pointers

while(fgets(line, line_max, file) != NULL){   // <=== Here is where valgrind complains !!!!

    line_len = strlen(line);
    if (line[line_len-1] == '\n'){

            strncpy(&line_full[line_full_len], line, line_len);
            line_full_len+=line_len;
    }
    else{

        //Check if line_full has enough memory left   
        strncpy(&line_full[line_full_len], line, line_len);
        line_full_len+=line_len;
    }
    line[0] = '\0';
}
free(line);
free(line_full);
fclose(file);
}

I keep getting the error:

 ==4929== Invalid read of size 32
 ==4929==    at 0x1003DDC1D: _platform_memchr$VARIANT$Haswell (in       /usr/lib/system/libsystem_platform.dylib)
 ==4929==    by 0x1001CF66A: fgets (in /usr/lib/system/libsystem_c.dylib)
 ==4929==    by 0x100000CD8: parse_one_file (main.c:93)
 ==4929==    by 0x100000B74: parse_files_dir (main.c:67)
 ==4929==    by 0x100000944: main (main.c:28)
 ==4929==  Address 0x100804dc0 is 32 bytes before a block of size 4,096 in arena "client"

So I really don’t see where my error is, I continue to empty the buffer line, I never read more bytes than are allocated there.

, , , "dirLink" , , , , , "full_path", "*" ( )

   parse_one_file("another/example/path/");

.

+4
2

1000 , line_full, 1000 . , , fgets.

+2
if(line[line_len-1] == '\n'){
  strncpy(&line_full[line_full_len], line, line_len);
  line_full_len+=line_len;
}

, strncpy() (line_max - line_full_len) , , line_len . . line_full [500], 1000 .

else.

+1

Source: https://habr.com/ru/post/1605966/


All Articles