Pregunta

I have a Loop checking the modified time of a file to perform i/o on it. I am using the stat command. Valgrind throws error messages of uninitialized bytes .. just what is wrong? I have ensured the file name list is not null and the files exist before passing them as param to it , yet the error persists.

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

    }

}

Error message

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

I am initializing the file names like this

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);
¿Fue útil?

Solución

As fileNameList is declared as:

char fileNameList[100][256];

if (fileNameList[i] != NULL) will always be true, as fileNameList[i] is not a null pointer. You should change the check to:

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

But, in order for that to work, you need to initialise fileNameList:

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

Otros consejos

If you didn't initialize the filename array first, its content won't be zeros, but something else (many times it's 0xCC, but it may differ on different systems / archs / etc.).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top