Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chdir does not work with/crashes when given relative paths #138

Closed
shinyquagsire23 opened this issue Jul 14, 2015 · 4 comments
Closed

chdir does not work with/crashes when given relative paths #138

shinyquagsire23 opened this issue Jul 14, 2015 · 4 comments

Comments

@shinyquagsire23
Copy link

While reworking my gpsp port's file code I found that chdir has a borked behavior in that it cannot chdir to relative file locations. As an example, doing a chdir("."); will actually cause ctrulib to crash to home. Same with chdir("..");. Even if you try to chdir to an exiting directory within the current working directory, it will crash. A simple test to check against this bug is just a chdir(".") after initializing sdmc.

Also, getcwd seems to return "/" initially rather than "sdmc:/", up until you explicitly chdir("sdmc:/"). Doesn't seem to affect anything, but it's there. I believe @mtheall is the one I should be talking with on this issue.

@mtheall
Copy link
Contributor

mtheall commented Jul 15, 2015

The newlib code is supposed to handle relative path components before passing a path down to the driver's code. It would be interesting to see what path is passed to sdmc_chdir().

@mtheall
Copy link
Contributor

mtheall commented Jul 15, 2015

So this is what I have found:

https://github.com/devkitPro/buildscripts/blob/master/dkarm-eabi/patches/newlib-2.2.0.patch#L779
We get here because the _current_working_directory is "/"

https://github.com/devkitPro/buildscripts/blob/master/dkarm-eabi/patches/newlib-2.2.0.patch#L782
pathPosition is assigned the value 0x1 because there is no ":" in "/"

https://github.com/devkitPro/buildscripts/blob/master/dkarm-eabi/patches/newlib-2.2.0.patch#L785
Obviously a segfault.

@mtheall
Copy link
Contributor

mtheall commented Jul 15, 2015

I think this would probably work:

        strncpy (temp_cwd, _current_working_directory, PATH_MAX);
    }

-   pathPosition = strchr (temp_cwd , ':') + 1;
+   pathPosition = strchr (temp_cwd , ':');
+   if (pathPosition == NULL)
+       pathPosition = temp_cwd;
+   else
+       ++pathPosition;

    /* Make sure the path starts in the root directory */
    if (pathPosition[0] != DIRECTORY_SEPARATOR_CHAR) {

@mtheall
Copy link
Contributor

mtheall commented Jul 15, 2015

For what it's worth, this has nothing to do with relative directories. You can recreate it by simply performing the first chdir() with any path which does not have a colon ":".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants