Readdir / closedir - Valgrind显示“无效读取”

时间:2011-08-06 02:10:01

标签: c linux valgrind readdir

在此处发布我的代码片段。我试图找到调试的基础。

struct dirent *s_dirent;
char path[300];
....
bzero(path,300);
...
fd_dir = opendir(path);
while((s_dirent = readdir(fd_dir))!=NULL)
{
     if(s_dirent->d_name[0] == '.')
          continue;
     else
          break;
 }
if(s_dirent == NULL)
{
   if(closedir(fd_dir)!=0)
       perror("Error on closedir");
 }
else
{

  if(closedir(fd_dir)!=0)/*Line number 249*/
      perror("Error on closedir");

  /*some comments*/
  strcat(path,"/");
  strcat(path,s_dirent->d_name);/*Line number 254*/
 }

Valgrind输出:

==3287== Invalid read of size 1
==3287==    at 0x40069E0: strcat (mc_replace_strmem.c:176)
==3287==    by 0x804D6B4: online_bck (backup_manager.c:254)
==3287==    by 0x8049F96: on_bck_beg (TxFS_manager.c:181)
==3287==    by 0x8049818: handler (Reader.c:236)
==3287==    by 0xBF5F18: start_thread (in /lib/libpthread-2.12.90.so)
==3287==    by 0xB37A2D: clone (in /lib/libc-2.12.90.so)
==3287==  Address 0x402a39b is 35 bytes inside a block of size 32,792 free'd
==3287==    at 0x40057F6: free (vg_replace_malloc.c:325)
==3287==    by 0xAF6C67: closedir (in /lib/libc-2.12.90.so)
==3287==    by 0x804D65A: online_bck (backup_manager.c:249)
==3287==    by 0x8049F96: on_bck_beg (TxFS_manager.c:181)
==3287==    by 0x8049818: handler (Reader.c:236)
==3287==    by 0xBF5F18: start_thread (in /lib/libpthread-2.12.90.so)
==3287==    by 0xB37A2D: clone (in /lib/libc-2.12.90.so)

任何帮助都将受到高度赞赏。 感谢

3 个答案:

答案 0 :(得分:4)

您不应该在调用readdir()后访问由closedir()返回的数据。这是因为closedir()可以释放在opendir / readdir中分配的任何资源(例如内存)。

如果要保存dirent * struct,可以切换到readdir的readdir_r变体(使用不同的参数集)。

更新:Valgrind输出的解码:

        V - note single space here; it is beginning of error message. 
==3287== Invalid read of size 1
==3287==    at 0x40069E0: strcat (mc_replace_strmem.c:176)

 backtrace skipped

Valgrind说错误是Reading of invalid data, sized 1 byte,这不是内存泄漏。这是不正确的内存访问。此读取的actor是strcat()(由跳过的回溯调用)。为什么数据无效?有submessage

        VV - note two spaces here, it is continuation of error message
==3287==  Address 0x402a39b is 35 bytes inside a block of size 32,792 free'd
==3287==    at 0x40057F6: free (vg_replace_malloc.c:325)
==3287==    by 0xAF6C67: closedir (in /lib/libc-2.12.90.so)

该字节无效(不允许从中读取),因为它是free-d的内存段的一部分(你不能从你自由的内存中读取-d)。这是谁?看看回溯:closedir是免费的来电者。

答案 1 :(得分:0)

s_dirent == NULL,检查while循环退出条件。

答案 2 :(得分:0)

当你到达strcat()时,你已经退出了while循环。 while s_dirent为NULL时,while循环退出。

但是只要s_direct为NULL,就运行它:

strcat(path,"/");
strcat(path,s_dirent->d_name);/*Line number 254*/

...试图取消引用s_dirent。因此,valgrind然后告诉你检查从backup_manager.c调用strcat()的调用:254:

==3287== Invalid read of size 1
==3287==    at 0x40069E0: strcat (mc_replace_strmem.c:176)
==3287==    by 0x804D6B4: online_bck (backup_manager.c:254)

我猜你只是在处理一个包含“。”,“..”条目的空目录或者只包含以“。”开头的隐藏文件的目录时才会出现这种情况?