Skip to content

Commit 014e26c

Browse files
iCharlesHuCharles Hu
andauthored
Add lock when retrieving ProcessInfo.environment (swiftlang#420)
Resolves: rdar://122515578 Co-authored-by: Charles Hu <charle@hu.codes>
1 parent f28d05a commit 014e26c

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ final class _ProcessInfo: Sendable {
4444
}
4545

4646
var environment: [String : String] {
47+
_platform_shims_lock_environ()
48+
defer {
49+
_platform_shims_unlock_environ()
50+
}
4751
var results: [String : String] = [:]
4852
guard var environments: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> =
4953
_platform_shims_get_environ() else {

Sources/_CShims/include/platform_shims.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
#include <libkern/OSThermalNotification.h>
2525
#endif
2626

27-
INTERNAL char ** _platform_shims_get_environ();
27+
INTERNAL char * _Nullable * _Nullable _platform_shims_get_environ();
28+
29+
INTERNAL void _platform_shims_lock_environ();
30+
INTERNAL void _platform_shims_unlock_environ();
2831

2932
#if __has_include(<libkern/OSThermalNotification.h>)
3033
typedef enum {
@@ -45,7 +48,7 @@ typedef enum {
4548
} _platform_shims_OSThermalPressureLevel;
4649

4750

48-
INTERNAL const char *_platform_shims_kOSThermalNotificationPressureLevelName();
51+
INTERNAL const char * _Nonnull _platform_shims_kOSThermalNotificationPressureLevelName();
4952
#endif
5053

5154
#endif /* CSHIMS_PLATFORM_SHIMS */

Sources/_CShims/platform_shims.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@
1919
extern char **environ;
2020
#endif
2121

22+
#if __has_include(<libc_private.h>)
23+
#import <libc_private.h>
24+
void _platform_shims_lock_environ() {
25+
environ_lock_np();
26+
}
27+
28+
void _platform_shims_unlock_environ() {
29+
environ_unlock_np();
30+
}
31+
#else
32+
void _platform_shims_lock_environ() { /* noop */ }
33+
void _platform_shims_unlock_environ() { /* noop */ }
34+
#endif
35+
2236
char **
2337
_platform_shims_get_environ()
2438
{

0 commit comments

Comments
 (0)