c linux stat param指向未初始化的字节?

时间:2012-02-28 11:24:59

标签: c linux error-handling valgrind stat

我有一个循环检查文件的修改时间以对其执行i / o。 我正在使用stat命令。 Valgrind抛出未初始化字节的错误消息..只是出了什么问题?我确保文件名列表不为空并且文件存在之前将它们作为参数传递给它,但错误仍然存​​在。

for (i = 0; i < fcount; i++) {
    if (modTimeList[i] == 0) {
        int statret = 0;
        if(fileNameList[i]!=NULL)
        statret = stat(fileNameList[i], &file_stat);  // error 
        if (statret == -1) {
            printf(" stat error at %d", i);
        } else {
            modTimeList[i] = file_stat.st_mtime;
            // process
        }
    } else {
        int statret2 = 0;
        if(fileNameList[i]!=NULL)
        statret2 = stat(fileNameList[i], &file_stat); // error
        if (statret2 == -1) {
            printf(" stat error at %d", i);
        } else {
            if (modTimeList[i] < file_stat.st_mtime) {
                // process
            }
        }

    }

}

错误消息

==5153== Syscall param stat64(file_name) points to uninitialised byte(s)
==5153==    at 0x40007F2: ??? (in /lib/ld-2.7.so)
==5153==    by 0x804992B: stat (in /home/)
==5153==    by 0x8049559: checkForFiles (in /home)
==5153==    by 0x804983F: main (in /home)
==5153==  Address 0xbe9271d0 is on thread 1's stack
==5153==  Uninitialised value was created by a stack allocation
==5153==    at 0x804924C: checkForFiles (in /home/)
==5153==

Decleration

char fileNameList[100][256];

我正在初始化像这样的文件名

sprintf(inputPath, "find -name %s*.ext", filename);
        fpop = popen(inputPath, "r");
        while (fgets(inputPath, sizeof(inputPath) - 1, fpop) != NULL) {
            strcpy(fileNameList[fcount], trimwhitespace(inputPath));
            fcount++;
        }
        pclose(fpop);

2 个答案:

答案 0 :(得分:2)

由于fileNameList被声明为:

char fileNameList[100][256];

if (fileNameList[i] != NULL)将始终为true,因为fileNameList[i]不是空指针。您应该将支票更改为:

if ('\0' != *fileNameList[i]) /* Check if empty string. */

但是,为了实现这一点,您需要初始化fileNameList

char fileNameList[100][256] = { { 0 } };

答案 1 :(得分:1)

如果你没有首先初始化filename数组,它的内容将不是零,而是其他内容(很多时候它是0xCC,但它可能在不同的系统/ archs /等上有所不同。) / p>