Skip to content

Commit 4bd6b75

Browse files
committed
Assume not having libc means this is not macOS
We know that libc will always be present on macOS, so if it is not there, we can assume this is not macOS. More generally, use a try/finally to avoid leaking the allocated buffer when libc is not found and set `checkOS` properly so we don't try to check again. This corrects Unity case 1038918, and in general helps our platform story int he class libraries a little bit, until we get better platform detection support at runtime.
1 parent d130c38 commit 4bd6b75

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

mcs/class/System/System/Platform.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,40 @@ private static void CheckOS() {
5959
}
6060

6161
IntPtr buf = Marshal.AllocHGlobal (8192);
62-
if (uname (buf) == 0) {
63-
string os = Marshal.PtrToStringAnsi (buf);
64-
switch (os) {
65-
case "Darwin":
66-
isMacOS = true;
67-
break;
68-
case "FreeBSD":
69-
isFreeBSD = true;
70-
break;
62+
try {
63+
if (uname (buf) == 0) {
64+
string os = Marshal.PtrToStringAnsi (buf);
65+
switch (os) {
66+
case "Darwin":
67+
isMacOS = true;
68+
break;
69+
case "FreeBSD":
70+
isFreeBSD = true;
71+
break;
72+
}
7173
}
7274
}
73-
Marshal.FreeHGlobal (buf);
74-
checkedOS = true;
75+
finally {
76+
Marshal.FreeHGlobal (buf);
77+
checkedOS = true;
78+
}
7579
}
7680
#endif
7781

7882
public static bool IsMacOS {
7983
get {
8084
if (!checkedOS)
85+
#if UNITY
86+
try {
87+
CheckOS();
88+
}
89+
catch (DllNotFoundException e) {
90+
// libc does not exist, so this is not MacOS
91+
isMacOS = false;
92+
}
93+
#else
8194
CheckOS();
95+
#endif
8296
return isMacOS;
8397
}
8498
}

0 commit comments

Comments
 (0)