【问题标题】:C Corrupted double-linked list after calling chdirC 调用 chdir 后损坏的双链表
【发布时间】:2016-06-08 17:05:08
【问题描述】:

我一直在编写一些 C 代码来提供类似于 shell 的功能。到目前为止,它可以运行大多数命令,如 cat、ls、ls | grep ...等。在编写了实现“cd”、“pwd”、“pushd”和“popd”功能的代码之后,我一直在测试它。如果我写“cd ..”或“cd anydir”,它将运行“chdir(anydir)”并返回到我的脚本命令提示符。在“cd”之后输入任何命令后都会返回错误:

*** Error in `./smshv4': corrupted double-linked list: 0x00000000018d5190 ***

我在一个不相关的变量中重新分配内存时隔离了要打印的这个错误。我已经评论了错误产生的那一行:

char * next_cmd(char *prompt, FILE *fp){
        fprintf(stderr,"in next_cmd\n");

        char    *buf ;
        int bufspace = 0;
        int pos = 0;
        int c;

        printf("%s", prompt);
        while( ( c = getc(fp)) != EOF ) {

                if( pos+1 >= bufspace ){
                        if ( bufspace == 0 ){
                        buf = emalloc(BUFSIZ);  //Error occurs while calling this function
                        } else  {
                                buf = erealloc(buf,bufspace+BUFSIZ);
                        }
                        bufspace += BUFSIZ;
                }

                /* end of command? */
                if ( c == '\n' )
                        break;

                /* no, add to buffer */
                buf[pos++] = c;
        }

        if ( c == EOF && pos == 0 )
               return NULL;
        buf[pos] = '\0';
        return buf;
}

此错误仅在我使用 chdir 运行任何函数后发生。这是我在输入 cd 时如何调用 chdir 的示例:

if(strcmp(cmdStruct.commandTemp[0],"cd") == 0){
    if(cmdStruct.commandTemp[1] != NULL){
        if(chdir(cmdStruct.commandTemp[1]) == -1){
            perror("chdir");
            return -1;
        }
    } else {
        fprintf(stderr,"cd failed. No directory given\n");
        return -1;
    }
}

这个 cd 命令不起作用,因为每当我运行它时,给定的错误将在我下次输入命令时显示。如果我 cd 到另一个目录,运行任何程序以接收错误,然后运行“pwd”(它正在工作),它将显示原始目录(输入一个命令后错误似乎自行解决)。

如何解决损坏的双链表问题?谢谢。

编辑:我调用命令提示符如下图:

while ( (cmdline = next_cmd(prompt, stdin)) != NULL ){  //command prompt function is run
    //if & sign detected, fork. otherwise run command as usual
    if(hasBackgroundSign(cmdline) == 1){
        pid = fork();
        if(pid == 0){
            if ( (arglist = splitline(cmdline)) != NULL  ){
                result = execute(arglist);
                freelist(arglist);
            }
            free(cmdline);
            kill(getpid(), SIGKILL);
        } else {
            printf("%d\n",pid);
        }
    } else {
        //if no & sign detected, run command as usual without forking
        if ( (arglist = splitline(cmdline)) != NULL  ){
            result = execute(arglist);  //execute function contains the chdir calls
            freelist(arglist);
        }
        free(cmdline);
    }
}

【问题讨论】:

  • "...回到我的脚本命令提示符" - 哪个脚本?为什么不显示呢?
  • 只需在 Valgrind 下运行你的 shell 并跟进所有报告的错误。
  • emalloc/erealloc不是标准函数。
  • 您的问题是内存损坏。您的帖子中未包含的代码可能会损坏内存。此类问题通常由内存调试器(例如 valgrind)解决。
  • 感谢您的回复,我包含了一些代码,显示了我如何调用 next_cmd 函数(提示输入)。我将尝试找出我的代码中可能发生内存损坏的地方。

标签: c linux bash shell chdir


【解决方案1】:

通过使用 Valgrind 查找损坏的内存位置来解决。 Valgrind 调用了“大小为 8 的无效写入”。解决此问题后,问题就消失了。 感谢 Leon 在上面提出这个建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多