0

I have a program which goes through the directory structure and concatenates the files present in the path to szFile . I have used dirent here to get the directory entries. It is dumping core in the strcat function inside the for loop only in SunOS . It goes through fine in HP and AIX machine .

#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <limits.h>
#include <sys/types.h>

int main()
{

    DIR            *pDirHand;

    char            szFile[1024];
    struct dirent   pdirent ;
    struct dirent *pResult = NULL;

    char *sDir = "fullpath"; /* fullpath can be /make/users/path */

    strncpy (szFile, sDir, sizeof(szFile)-1);
    szFile[sizeof(szFile)-1] = '\0';

    if (NULL == (pDirHand = opendir(szFile)))
    {
        return -1;
    }

    for(readdir_r(pDirHand, &pdirent, &pResult); pResult != 0;readdir_r(pDirHand, &pdirent, &pResult))
    {
        FILE *fp;
        fp=fopen("debug.log","a+");
        strcpy (szFile, sDir);

        strcat (szFile, "/");

        strcat (szFile, pdirent.d_name);
    }

    if (pDirHand) closedir (pDirHand);

    return 0;
}

I dont have any files currently in the path that I assign to sDir. It has "." and ".." directory entries in it but I get a core dump in the line

strcat (szFile, pdirent.d_name);

I had used dbx to find out the value of szFile , during the second iteration the value is exceeding the memory allocated for it . The value comes as

"fullpath/../fullpath/../fullpath/../fullpath/../fullpath/..fullpath/..fullpath/../../../../../../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/..fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/../fullpath/.." ...

I have tried using strlcat , but the concatinated value of szFile is not coming properly. Anybody faced this problem in SunOS or can help ?

tristan
  • 4,235
  • 2
  • 21
  • 45
user3563852
  • 23
  • 1
  • 7
  • Use `snprintf(szFile, sizeof(szFile), ` ... – Basile Starynkevitch Nov 14 '14 at 06:29
  • You can add -D_POSIX_PTHREAD_SEMANTICS to your compilation command to enable POSIX semantics. – tristan Nov 14 '14 at 06:56
  • 2
    If I were to venture a guess - SunOS probably uses a flexible array for `d_name`. In essence the entire pdirent variable isn't actually big enough to hold the actual filename. This kind of behavior is documented and the portable mechanism to create a `dirent` structure can be found near the bottom of this [link](http://linux.die.net/man/3/readdir_r) . This isn't a problem on all platforms because in some instances `d_name` is defined as an array with ability to hold the maximum length of a filename. – Michael Petch Nov 14 '14 at 07:46
  • 1
    There's never a length check on the strings, which is always a bit risky. The final `if (pDirHand) closedir(pDirHand);` test is unnecessary; you've exited already if the handle was null (but that doesn't affect the crash). You open the debug log file once for each file name in the directory, but never use the file handle, nor close it. This leaks resources on a large scale, but again is unlikely to be the cause of a crash. – Jonathan Leffler Nov 14 '14 at 15:11

0 Answers0