Valgrind ::有条件的跳跃或移动取决于未初始化的值(s)

时间:2014-10-06 01:24:40

标签: c valgrind

当我在我的软件上运行valgrind时,它会给出条件跳转或移动取决于未初始化的值(s)错误。 valgrind的输出如下:

==17787== Conditional jump or move depends on uninitialised value(s)
==17787==    at 0x402688: directory_findname (directory.c:36)
==17787==    by 0x402750: directory_findname (directory.c:41)
==17787==    by 0x402038: pathname_lookup (pathname.c:28)
==17787==    by 0x402239: chksumfile_bypathname (chksumfile.c:55)
==17787==    by 0x4011F9: DumpPathAndChildren (diskimageaccess.c:143)
==17787==    by 0x4014E9: DumpPathAndChildren (diskimageaccess.c:182)
==17787==    by 0x4014E9: DumpPathAndChildren (diskimageaccess.c:182)
==17787==    by 0x40155D: DumpPathnameChecksum (diskimageaccess.c:193)
==17787==    by 0x400F39: main (diskimageaccess.c:80)
==17787==  Uninitialised value was created by a stack allocation
==17787==    at 0x402576: directory_findname (directory.c:27)
==17787== 
==17787== Conditional jump or move depends on uninitialised value(s)
==17787==    at 0x4C2F1BC: strcmp (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17787==    by 0x4026B6: directory_findname (directory.c:37)
==17787==    by 0x402750: directory_findname (directory.c:41)
==17787==    by 0x402038: pathname_lookup (pathname.c:28)
==17787==    by 0x402239: chksumfile_bypathname (chksumfile.c:55)
==17787==    by 0x4011F9: DumpPathAndChildren (diskimageaccess.c:143)
==17787==    by 0x4014E9: DumpPathAndChildren (diskimageaccess.c:182)
==17787==    by 0x4014E9: DumpPathAndChildren (diskimageaccess.c:182)
==17787==    by 0x40155D: DumpPathnameChecksum (diskimageaccess.c:193)
==17787==    by 0x400F39: main (diskimageaccess.c:80)
==17787==  Uninitialised value was created by a stack allocation
==17787==    at 0x402576: directory_findname (directory.c:27)
==17787== 
==17787== Conditional jump or move depends on uninitialised value(s)
==17787==    at 0x4026B9: directory_findname (directory.c:37)
==17787==    by 0x402750: directory_findname (directory.c:41)
==17787==    by 0x402038: pathname_lookup (pathname.c:28)
==17787==    by 0x402239: chksumfile_bypathname (chksumfile.c:55)
==17787==    by 0x4011F9: DumpPathAndChildren (diskimageaccess.c:143)
==17787==    by 0x4014E9: DumpPathAndChildren (diskimageaccess.c:182)
==17787==    by 0x4014E9: DumpPathAndChildren (diskimageaccess.c:182)
==17787==    by 0x40155D: DumpPathnameChecksum (diskimageaccess.c:193)
==17787==    by 0x400F39: main (diskimageaccess.c:80)
==17787==  Uninitialised value was created by a stack allocation
==17787==    at 0x402576: directory_findname (directory.c:27)
==17787== 

代码本身运行良好,并提供了我期望的所有输出。另外valgrind只在我的软件选择的几个输入上给我这个错误。在大多数输入中,我没有得到任何错误。有问题的代码部分是:

 const int direntPerBlock = DISKIMG_SECTOR_SIZE/sizeof(dirEnt);
  struct direntv6 buf[direntPerBlock];
  int inodeSize = inode_getsize(&in);
  int ttlBlockNum = inode_getsize(&in)/DISKIMG_SECTOR_SIZE + 1;

  int j;
  for(j = 0; j < ttlBlockNum; j++){
    if(diskimg_readsector(fs->dfd, inode_indexlookup(fs, &in, j), buf)){
      for(i = 0; i < direntPerBlock; i++){
        if(buf[i].d_name[0] != '\0'){
          if(strcmp(buf[i].d_name, dirName)==0){
            if(strlen(name) == strlen(dirName))
              return buf[i].d_inumber;
            else
              return directory_findname(fs, name+(strlen(dirName)+1)*sizeof(char), buf[i].d_inumber, dirEnt);
          }
        }
      }
    }
  }

第27行,即假定的未经宣传的值,是struct direntv6 buf[direntPerBlock];。第36和37行是

    if(buf[i].d_name[0] != '\0'){
      if(strcmp(buf[i].d_name, dirName)==0){

函数diskimg_readsector(...)接受buf并用适当的数据填充它。该功能实现为:

int diskimg_readsector(int fd, int sectorNum,  void *buf) {
  if (lseek(fd, sectorNum * DISKIMG_SECTOR_SIZE, SEEK_SET) == (off_t) -1) return -1;
  return read(fd, buf, DISKIMG_SECTOR_SIZE);
}

为什么valgrind偶尔会给我这个问题的任何线索将不胜感激。

编辑:

`DIRNAME&#39;被发现为:

  // extract the name of the current root directory                                                      
  char dirName[sizeof(dirEnt->d_name)];
  int i = 0;
  while(name[i] != '/' && name[i] != '\0'){
    dirName[i] = name[i];
    i++;
  }
  dirName[i] = '\0';

编辑**:

问题已经消失。我刚补充说:

struct direntv6 buf[direntPerBlock];
memset(buf, 0, sizeof(buf));

1 个答案:

答案 0 :(得分:3)

尝试初始化您的数据:

struct direntv6 buf[direntPerBlock] = {0};

这样它永远不会被初始化,并且内部未设置的字符串应该显示为空而不是内部有垃圾。我想问题可能在于你如何使用diskimg_readsector,但我没有看到代码,因此无法确定。