From 0d2160b3d8fd7fa6263e2ff28a5f1f8f9f985c52 Mon Sep 17 00:00:00 2001 From: Elliot Saba Date: Wed, 5 Jan 2022 10:44:05 -0800 Subject: [PATCH] Honor `$TEMP` and friends when generating anonymous handles (#43515) On Darwin, when generating anonymous handles, we hardcoded `/tmp` as the beginning path, but that's bad behavior if the user has explicitly requested (via setting `$TEMP` or `$TEMPDIR`, etc...) that we not do so. Let's use `uv_os_tmpdir()` to put things in the correct place as much as we can, defaulting to `/tmp` if anything goes wrong inside of libuv. --- src/cgmemmgr.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/cgmemmgr.cpp b/src/cgmemmgr.cpp index fabeb8b62e747..fcaf77ed6e0b9 100644 --- a/src/cgmemmgr.cpp +++ b/src/cgmemmgr.cpp @@ -173,7 +173,7 @@ static intptr_t get_anon_hdl(void) if (check_fd_or_close(fd)) return fd; # endif - char shm_name[] = "julia-codegen-0123456789-0123456789/tmp///"; + char shm_name[PATH_MAX] = "julia-codegen-0123456789-0123456789/tmp///"; pid_t pid = getpid(); // `shm_open` can't be mapped exec on mac # ifndef _OS_DARWIN_ @@ -195,8 +195,14 @@ static intptr_t get_anon_hdl(void) return fd; } } - snprintf(shm_name, sizeof(shm_name), - "/tmp/julia-codegen-%d-XXXXXX", (int)pid); + size_t len = sizeof(shm_name); + if (uv_os_tmpdir(shm_name, &len) != 0) { + // Unknown error; default to `/tmp` + snprintf(shm_name, sizeof(shm_name), "/tmp"); + len = 4; + } + snprintf(shm_name + len, sizeof(shm_name) - len, + "/julia-codegen-%d-XXXXXX", (int)pid); fd = mkstemp(shm_name); if (check_fd_or_close(fd)) { unlink(shm_name);