fix(native): allocated getcwd and realpath results name the guest mount (#3481)

getcwd(NULL, ...) and realpath(path, NULL) returned the host path of a
session mount whenever its guest path was longer than the host path,
because the translation had to fit the buffer the C library allocated.
Shells export that host path as PWD, and Wine prefers a valid PWD, so a
program started from an added game folder on a removable card got a
working directory it could not open and started in the Windows folder.
Grow the allocated result to fit the guest path.
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-30 14:24:10 -05:00
1 parent 7404fba5a3
commit 5f129fb4db
3 files changed
+43 -5

No files matched your search

+4 -5
View File
@@ -739,10 +739,8 @@ char *getcwd(char *buf, size_t size) {
static char *(*orig_getcwd)(char *, size_t) = NULL;
if (!orig_getcwd) orig_getcwd = (char *(*)(char *, size_t))dlsym(RTLD_NEXT, "getcwd");
char *res = orig_getcwd ? orig_getcwd(buf, size) : NULL;
if (res) {
size_t len = strlen(res);
guest_path_of_mount_source(res, len, buf ? size : len + 1, NULL);
}
if (res && !buf) return guest_path_of_allocated_mount_source(res);
if (res) guest_path_of_mount_source(res, strlen(res), size, NULL);
return res;
}
@@ -790,7 +788,8 @@ char *realpath(const char *path, char *resolved_path) {
}
}
}
if (res) guest_path_of_mount_source(res, strlen(res), resolved_path ? PATH_MAX : strlen(res) + 1, NULL);
if (res && !resolved_path) return guest_path_of_allocated_mount_source(res);
if (res) guest_path_of_mount_source(res, strlen(res), PATH_MAX, NULL);
return res;
}
+14
View File
@@ -975,6 +975,20 @@ static size_t guest_path_of_mount_source(char *buf, size_t len, size_t cap, int
return best->dst_len + rest;
}
static char *guest_path_of_allocated_mount_source(char *res) {
size_t len = strlen(res);
char tmp[PATH_MAX];
if (len >= sizeof(tmp)) return res;
memcpy(tmp, res, len + 1);
int converted = 0;
size_t n = guest_path_of_mount_source(tmp, len, sizeof(tmp), &converted);
if (!converted) return res;
char *grown = realloc(res, n + 1);
if (!grown) return res;
memcpy(grown, tmp, n + 1);
return grown;
}
static ssize_t postprocess_readlink(const char *linkpath, char *buf, ssize_t ret, size_t bufsiz) {
if (ret <= 0) return ret;
if ((size_t)ret < bufsiz) {
+25
View File
@@ -88,6 +88,18 @@ int main(int argc, char **argv) {
printf("%s\t%s\n", argv[i], ok ? cwd : "-");
continue;
}
if (strncmp(argv[i], "cdnull:", 7) == 0) {
char *got = chdir(argv[i] + 7) == 0 ? getcwd(NULL, 0) : NULL;
printf("%s\t%s\n", argv[i], got ? got : "-");
free(got);
continue;
}
if (strncmp(argv[i], "realnull:", 9) == 0) {
char *got = realpath(argv[i] + 9, NULL);
printf("%s\t%s\n", argv[i], got ? got : "-");
free(got);
continue;
}
if (strncmp(argv[i], "lstat:", 6) == 0) {
int ok = lstat(argv[i] + 6, &st) == 0;
printf("%s\t%d\t%d\n", argv[i], ok, ok && S_ISLNK(st.st_mode));
@@ -324,6 +336,19 @@ class NativeHookMountTest(unittest.TestCase):
arg = f"cd:/pfx/native/dosdevices/z:{GAME}/"
self.assertEqual(self.run_probe(arg)[arg], [GAME])
def test_allocated_working_directory_and_real_path_name_a_longer_guest_mount(self):
guest = "/root/Games/" + "g" * len(str(self.library))
binds = f"{self.library}:{guest}"
args = [f"cd:{guest}/Some Game", f"cdnull:{guest}/Some Game", f"realnull:{guest}/Some Game/game.txt"]
result = self.run_probe(*args, binds=binds)
self.assertEqual(result[args[0]], [f"{guest}/Some Game"])
self.assertEqual(result[args[1]], [f"{guest}/Some Game"])
self.assertEqual(result[args[2]], [f"{guest}/Some Game/game.txt"])
def test_allocated_working_directory_names_a_shorter_guest_mount(self):
arg = f"cdnull:{GAME}"
self.assertEqual(self.run_probe(arg)[arg], [GAME])
def test_rootfs_path_under_a_mount_point_reaches_the_mount(self):
path = f"{self.root}{GAME}/game.txt"
self.assertEqual(self.run_probe(path)[path], ["1", "library"])