From a8fe20a1e2c3ca178325978c6fc3501b9670c052 Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 28 Mar 2025 10:42:55 -0700 Subject: [PATCH] windows: Fix insecure CRT warning about getenv. --- PythonKit/PythonLibrary.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/PythonKit/PythonLibrary.swift b/PythonKit/PythonLibrary.swift index 6149478..b689b39 100644 --- a/PythonKit/PythonLibrary.swift +++ b/PythonKit/PythonLibrary.swift @@ -284,7 +284,17 @@ extension PythonLibrary { } var value: String? { +#if os(Windows) + var cString: UnsafeMutablePointer? = nil + var size: size_t = 0 + let result = _dupenv_s(&cString, &size, key) + defer { + if let cString { free(cString) } + } + guard result == 0, let cString else { return nil } +#else guard let cString = getenv(key) else { return nil } +#endif let value = String(cString: cString) guard !value.isEmpty else { return nil } return value