From 405ce6e5f542b91b7aac81c3ed76bdd2c7ff329c Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Thu, 9 May 2024 12:32:06 -0600 Subject: [PATCH] LibCore: Be more BSD-friendly in Core::Environment FreeBSD and NetBSD don't have secure_getenv(3), same as macOS. FreeBSD 13 and lower also don't allow setting environ pointers to null. Co-Authored-By: Robert Clausecker --- Userland/Libraries/LibCore/Environment.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibCore/Environment.cpp b/Userland/Libraries/LibCore/Environment.cpp index aa0952cb190..f0fc06a8845 100644 --- a/Userland/Libraries/LibCore/Environment.cpp +++ b/Userland/Libraries/LibCore/Environment.cpp @@ -13,7 +13,7 @@ #if defined(AK_OS_MACOS) || defined(AK_OS_IOS) # include #else -extern char** environ; +extern "C" char** environ; #endif namespace Core::Environment { @@ -93,15 +93,16 @@ Optional get(StringView name, [[maybe_unused]] SecureOnly secure) builder.append('\0'); // Note the explicit null terminators above. -#if defined(AK_OS_MACOS) || defined(AK_OS_ANDROID) - char* result = ::getenv(builder.string_view().characters_without_null_termination()); -#else + // FreeBSD < 14, Android, and generic BSDs do not support secure_getenv. +#if (defined(__FreeBSD__) && __FreeBSD__ >= 14) || (!defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_ANDROID)) char* result; if (secure == SecureOnly::Yes) { result = ::secure_getenv(builder.string_view().characters_without_null_termination()); } else { result = ::getenv(builder.string_view().characters_without_null_termination()); } +#else + char* result = ::getenv(builder.string_view().characters_without_null_termination()); #endif if (result) return StringView { result, strlen(result) }; @@ -153,7 +154,9 @@ ErrorOr put(StringView env) ErrorOr clear() { -#if defined(AK_OS_MACOS) +#if (defined(__FreeBSD__) && __FreeBSD__ < 14) + environ = nullptr; +#elif defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_FREEBSD) auto environment = raw_environ(); for (size_t environ_size = 0; environment[environ_size]; ++environ_size) { environment[environ_size] = NULL;