ディレクトリ構造を調べて、パスに存在するファイルを szFile に連結するプログラムがあります。ここでは dirent を使用してディレクトリ エントリを取得しました。SunOS でのみ for ループ内の strcat 関数でコアをダンプしています。HP および AIX マシンでは問題なく実行されます。
#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;
}
現在、sDir に割り当てたパスにファイルがありません。「。」があります。および「..」ディレクトリエントリが含まれていますが、行にコアダンプが表示されます
strcat (szFile, pdirent.d_name);
dbx を使用して szFile の値を調べましたが、2 回目の反復中に値が割り当てられたメモリを超えています。値は次のようになります
"フルパス/../フルパス/../フルパス/../フルパス/../フルパス/..フルパス/..フルパス/../../../../../../フルパス/../フルパス/../フルパス/../フルパス/../フルパス/../フルパス/../フルパス/../フルパス/../フルパス/../フルパス/..フルパス/ ../フルパス/../フルパス/../フルパス/../フルパス/../フルパス/../フルパス/../フルパス/../フルパス/../フルパス/.." .. .
strlcat を使用してみましたが、 szFile の連結値が正しく表示されません。SunOS でこの問題に直面した人、または助けてくれる人はいますか?