#GP4 tester error (still)

I’m stuck on Stage #GP4.

I’m still having the issue from last time #GP4 tester error

I didn’t had the chance to reply last time, and didn’t be active on codecrafters since then.
The thing is that my code already is expanding “~” to the full path as suggested

I tried to disable the helper function that expands the tilde and explicitly do it when handling the CD command without success.

Here are my logs:

[tester::#GP4] Running tests for Stage #GP4 (Navigation - The cd builtin: Home directory)
[tester::#GP4] Running ./your_program.sh
[your-program] $ cd /tmp/banana/pear/banana
[your-program] $ pwd
[your-program] /tmp/banana/pear/banana
[tester::#GP4] Received current working directory response
[your-program] $ cd ~
[your-program] $ pwd
[your-program] malloc(): invalid next size (unsorted)
[tester::#GP4] ^ Line does not match expected value.
[tester::#GP4] Expected: "/tmp/pineapple/apple/apple"
[tester::#GP4] Received: "malloc(): invalid next size (unsorted)"
[tester::#GP4] Assertion failed.
[tester::#GP4] Test failed

And here’s a snippet of my code:

I call this helper function:

        msh_performExpansions(tokens, token_count);

which is:

void msh_performExpansions(char **tokens, int token_count)
{
    for (int i=0; i < token_count; i++) {

        if (!strcmp(tokens[i], "~")) {
        #ifdef _WIN32
            strcpy(tokens[i], getenv("USERPROFILE"));
        #else
            strcpy(tokens[i], getenv("HOME"));
        #endif
        }
    }
}

and handling the cd command:

        // CD COMMAND
        else if (strcmp(cmd.command, cd_command) == 0) {
            if (token_count == 1) {
                printf("You need to specify a directory\n");
            } else {
                
                if (!strcmp(cmd.args[1], "~")) {
                    strcpy(cmd.args[1], getenv("HOME"));
                }

                int result = chdir(cmd.args[1]);
                if ((result != 0) && (errno == ENOENT)) {
                    printf("cd: %s: No such file or directory\n", cmd.args[1]);
                }
            }
        }

Hope you can help me get through this,
Thanks,

I rushed… it seems that

strcpy(tokens[i], getenv("HOME"));

turned out to be a bad idea—getenv("HOME") can be longer than tokens[i], which could mess up memory. All fixed now, though—it’s working fine.

1 Like