0

ファイルの変更時刻をチェックして I/O を実行するループがあります。stat コマンドを使用しています。Valgrind は、初期化されていないバイトのエラー メッセージをスローします。何が問題なのですか? ファイル名リストがnullではなく、paramとして渡す前にファイルが存在することを確認しましたが、エラーは解決しません。

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

宣言

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

2 に答える 2

2

としてfileNameList宣言されているように:

char fileNameList[100][256];

if (fileNameList[i] != NULL)fileNameList[i]nullポインタではないため、常にtrueになります。チェックを次のように変更する必要があります。

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

しかし、それが機能するためには、初期化する必要がありますfileNameList

char fileNameList[100][256] = { { 0 } };
于 2012-02-28T11:50:13.587 に答える
1

最初に配列を初期化しなかった場合filename、その内容はゼロではなく、別のものになります (多くの場合、0xCC ですが、システムやアーキテクチャなどによって異なる場合があります)。

于 2012-02-28T11:29:13.037 に答える