C 为什么opendir会出现SIGABRT错误

C 为什么opendir会出现SIGABRT错误,c,unix,posix,C,Unix,Posix,我在使用opendir打开程序中的目录时遇到问题 我用这句话来做这件事: int main(int argc, char** argv) { int i; int Port,TPS,QS,Height,Number_kids; char server_ip[16]; DIR * directory; struct ps *parms = (struct ps*)malloc(sizeof(struct ps)); if(argc!=8)

我在使用opendir打开程序中的目录时遇到问题

我用这句话来做这件事:

int main(int argc, char** argv) 
{
    int i;
    int Port,TPS,QS,Height,Number_kids;
    char server_ip[16];
    DIR * directory;
    struct ps *parms = (struct ps*)malloc(sizeof(struct ps));

    if(argc!=8)
    {
        printf("Wrong number of arguents given!!!\n");
        return -1;
    }

    Port=atoi(argv[2]);
    TPS=atoi(argv[3]);
    QS=atoi(argv[4]);
    Height=atoi(argv[5]);
    Number_kids=atoi(argv[6]);

    strcpy(server_ip,argv[1]);
    strcpy(parms->directory,argv[7]);


    if(Height<2 || Number_kids<2)
    {
        printf("Wrong parameters given!!!\n");
        return -1;
    }

    for(i=2;i<7;i++)
    {
        int temp=atoi(argv[i]);
        if(temp<=0)
        {
            printf("Wrong parameters given!!!\n");
            return -1;
        }
    }

    directory = opendir(parms->directory);
    if(directory == NULL)
    {
        perror("Error when trying to open specified directory:");
        return;
    }
}
目录函数包含一个目录路径:/home/user/Desktop/folder (路径正确)

当我在没有调试器的情况下运行程序时,出现以下错误:

    malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) *
 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) 
(old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof
(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)
old_end & pagemask) == 0)' failed.
我可以从中看出,当函数opendir使用malloc(?)时,它内部出现了一些错误

当我运行调试器时,我得到一个SIGABRT信号和相同的错误。(我正在使用Netbeans)

我不知道我在这方面做错了什么。非常感谢您的帮助。

如果
malloc()
以这种方式失败,那么几乎可以肯定这是您程序中的一个错误,并且该错误几乎肯定在其他地方。这个bug可能在一段完全不相关的代码中


这些错误通常是由内存损坏引起的。使用或在启用的情况下编译程序。这些都必须安装,但如果您使用C编写代码,它们是工具箱中必不可少的一部分。它们更有可能指出程序中实际导致内存损坏的部分。

我使用了valgrind,我的其余代码没有任何问题。我似乎在opendir函数中有一个堆损坏。我并没有在那个节目中做一些非常复杂的事情。它只是一个试图打开的主函数。如果它真的只是一个
main()
函数,请发布整个程序。你可以发布到Gist()或将整个程序粘贴到你的问题中,这取决于它的长度。@Blenikos:我注意到你的代码对不可信的输入使用了
strcpy()
,这是不安全的。你确定strlen(argv[1])是的,我非常确定。这只是一个ip4地址。所以“\0”字符是3*4=12+1
    malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) *
 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) 
(old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof
(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)
old_end & pagemask) == 0)' failed.