From 3c569c00dfb4e5fd93a4401bdc307eca6b29102e Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 9 Mar 2022 11:06:39 +0100 Subject: [PATCH] macos: don't use thread-unsafe strtok() (#3524) Co-authored-by: Jameson Nash --- src/unix/process.c | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/unix/process.c b/src/unix/process.c index 1157006d..3c0f19f4 100644 --- a/src/unix/process.c +++ b/src/unix/process.c @@ -387,30 +387,22 @@ static void uv__spawn_init_posix_spawn_fncs(void) { static void uv__spawn_init_can_use_setsid(void) { - static const int MACOS_CATALINA_VERSION_MAJOR = 19; - char version_str[256]; - char* version_major_str; - size_t version_str_size = 256; - int r; - int version_major; + int which[] = {CTL_KERN, KERN_OSRELEASE}; + unsigned major; + unsigned minor; + unsigned patch; + char buf[256]; + size_t len; - /* Get a version string */ - r = sysctlbyname("kern.osrelease", version_str, &version_str_size, NULL, 0); - if (r != 0) + len = sizeof(buf); + if (sysctl(which, ARRAY_SIZE(which), buf, &len, NULL, 0)) return; - /* Try to get the major version number. If not found - * fall back to the fork/exec flow */ - version_major_str = strtok(version_str, "."); - if (version_major_str == NULL) + /* NULL specifies to use LC_C_LOCALE */ + if (3 != sscanf_l(buf, NULL, "%u.%u.%u", &major, &minor, &patch)) return; - /* Parse the version major as a number. If it is greater than - * the major version for macOS Catalina (aka macOS 10.15), then - * the POSIX_SPAWN_SETSID flag is available */ - version_major = atoi_l(version_major_str, NULL); /* Use LC_C_LOCALE */ - if (version_major >= MACOS_CATALINA_VERSION_MAJOR) - posix_spawn_can_use_setsid = 1; + posix_spawn_can_use_setsid = (major >= 19); /* macOS Catalina */ }