Skip to content
This repository was archived by the owner on Jun 14, 2022. It is now read-only.

Commit 4214b5a

Browse files
committed
Better way to parse system versoin on macOS
1 parent fd04a0d commit 4214b5a

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/psync/pdeviceid.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <winuser.h>
2020
#include <windows.h>
2121
#elif defined(P_OS_MACOSX)
22+
#include <errno.h>
23+
#include <limits.h>
2224
#include <stdlib.h>
2325
#include <sys/utsname.h>
2426
#include <sys/sysctl.h>
@@ -88,7 +90,20 @@ static char *psync_detect_os_name() {
8890
if (uname(&un))
8991
ver = "macOS";
9092
else {
91-
v = atoi(un.release);
93+
v = strtol(un.release, &endptr, 10);
94+
/* out of range, not X.Y.Z, no conversion at all */
95+
if (errno == ERANGE || *endptr != '.' || un.release == endptr) {
96+
log_error("failed determine OS release: %s", strerror(errno));
97+
v = 0;
98+
}
99+
#if LONG_MIN < INT_MIN || LONG_MAX > INT_MAX
100+
else if (v < INT_MIN || v > INT_MAX) {
101+
errno = ERANGE;
102+
log_error("failed determine OS release: %s", strerror(errno));
103+
v = 0;
104+
}
105+
#endif
106+
92107
switch (v) {
93108
case 21: ver="macOS 12 Monterey"; break;
94109
case 20: ver="macOS 11 Big Sur"; break;

0 commit comments

Comments
 (0)