Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix SunOS中的strcat卸料堆芯_Unix_Segmentation Fault_Strcat_Sunos - Fatal编程技术网

Unix SunOS中的strcat卸料堆芯

Unix SunOS中的strcat卸料堆芯,unix,segmentation-fault,strcat,sunos,Unix,Segmentation Fault,Strcat,Sunos,我有一个程序,它遍历目录结构并连接到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() {

我有一个程序,它遍历目录结构并连接到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;
}
#包括
#包括
#包括
#包括
#包括
int main()
{
DIR*pDirHand;
字符文件[1024];
结构导向;
struct dirent*pResult=NULL;
char*sDir=“fullpath”/*fullpath可以是/make/users/path*/
strncpy(szFile,sDir,sizeof(szFile)-1);
szFile[sizeof(szFile)-1]='\0';
if(NULL==(pDirHand=opendir(szFile)))
{
返回-1;
}
for(readdir\u r(pDirHand,&pdirent,&preslt);preslt!=0;readdir\u r(pDirHand,&pdirent,&preslt))
{
文件*fp;
fp=fopen(“debug.log”,“a+”);
strcpy(szFile,sDir);
strcat(szFile,“/”);
strcat(szFile,pdirent.d_名称);
}
if(pDirHand)closedir(pDirHand);
返回0;
}
我当前在分配给sDir的路径中没有任何文件。它有“.”和“.”目录项,但我得到了一个核心转储行

strcat(szFile,pdirent.d_名称)

我使用dbx来找出szFile的值,在第二次迭代中,该值超过了为其分配的内存。其价值如下所示

“fullpath/。/fullpath/。/fullpath/。/fullpath/。/fullpath/。/../../../../../../fullpath/。/fullpath/。/fullpath/。/fullpath/。/fullpath/。/fullpath/。/fullpath/。/fullpath/。/fullpath/。/fullpath/。/fullpath/。/fullpath/。”

我已经尝试使用strlcat,但是szFile的concatative值没有正确地显示出来。
有人在SunOS遇到过这个问题,或者可以帮忙吗

使用
snprintf(szFile,sizeof(szFile),
…您可以在编译命令中添加-dPOSIX_PTHREAD_语义以启用POSIX语义。如果我大胆猜测,SunOS可能会使用灵活的数组来表示
D_name
。本质上,整个pDirect变量实际上不够大,无法容纳实际的文件名。这种行为是有文档记录的,可移植的mec创建
dirent
结构的方法可以在下面找到。这在所有平台上都不是问题,因为在某些情况下,
d_name
被定义为一个数组,能够保存文件名的最大长度。字符串从不进行长度检查,这总是有点危险。如果(pDirHand)closedir(pDirHand);test是不必要的;如果句柄为null,您已经退出(但这不会影响崩溃)。您为目录中的每个文件名打开调试日志文件一次,但从不使用文件句柄,也不关闭它。这会大量泄漏资源,但也不太可能是崩溃的原因。