Skip to content
Advertisement

C Corrupted double-linked list after calling chdir

I’ve been working on some C code to provide functionality similar to the shell. So far it can run most commands like cat, ls, ls | grep …, etc. After writing code to implement the functions “cd”, “pwd”, “pushd” and “popd”, I have been testing it. If I write “cd ..” or “cd anydir” it will run “chdir(anydir)” and return back to my scripts command prompt. After entering any command after “cd”, it will return the error:

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

I isolated this error to be printed while reallocating memory in an unrelated variable. I have commented on the line which the error is produced during:

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

        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] = '';
        return buf;
}

This error only occurs after I run any functions using chdir. Here is an example of my how I am calling chdir when cd is given as input:

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 givenn");
        return -1;
    }
}

This cd command isn’t working because whenever I run it, the error given will be shown the next time I enter a command. If I cd to a different directory, run anything to receive the error, and then run “pwd” (which is working), it will show the original directory (the error seems to sort itself out after entering one command).

How do I solve the corrupted double-linked list problem? Thanks.

Edit: I call the command prompt as shown below:

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("%dn",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);
    }
}

Advertisement

Answer

Solved by finding corrupt memory location, using Valgrind. “Invalid write of size 8” was called by Valgrind. After fixing this, the problem had gone. Thanks Leon for suggesting this above.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement