<分区>
我的代码中不断出现 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/");
错误依然存在..