使用C中的fgets逐行读取文件,读取大小无效

时间:2015-09-04 15:02:11

标签: c valgrind

我的代码中一直出现valgrind错误,三个小时后我仍然无能为力,所以我需要你帮助的人。

所以我基本上只是读取目录中包含的文件并解析它们,所以我复制了我的代码的最短例子仍然产生错误:

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);
}

所以现在实际的问题方法是:

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);
}

我一直收到错误:

 ==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"

所以我真的不知道我的错误在哪里,我一直在清空缓冲行,我从来没有读过比分配更多的字节。

我注意到的有趣的事情是,如果目录“dirLink”只有一个文件,则不会发生错误,但是如果我有两个或更多,则会发生错误,所以我认为错误是我如何生成路径“full_path”,但后来我用(仅出于测试原因)

替换了行“*”
   parse_one_file("another/example/path/");

并且错误仍然存​​在..

2 个答案:

答案 0 :(得分:2)

除非您的文件总共少于1000个字节,否则您将在line_full缓冲区的末尾写入,该缓冲区的大小总共只有1000个字节。这将永远破坏你的记忆,并导致像你在fgets中遇到的虚假错误。

答案 1 :(得分:1)

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分支中。