Skip to content

Commit c8db04c

Browse files
committed
JDK-8253839: Do not use objc_msgSend_stret
ZULU-17145
1 parent 620dadf commit c8db04c

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/java.base/macosx/native/libjava/java_props_macosx.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,23 +222,42 @@ char *setupMacOSXLocale(int cat) {
222222
}
223223
}
224224

225+
// 10.9 SDK does not include the NSOperatingSystemVersion struct.
226+
// For now, create our own
227+
typedef struct {
228+
NSInteger majorVersion;
229+
NSInteger minorVersion;
230+
NSInteger patchVersion;
231+
} OSVerStruct;
232+
225233
void setOSNameAndVersion(java_props_t *sprops) {
226234
// Hardcode os_name, and fill in os_version
227235
sprops->os_name = strdup("Mac OS X");
228236

229237
char* osVersionCStr = NULL;
230-
// Mac OS 10.2 includes the [NSProcessInfo operatingSystemVersionString] function,
231-
// but it's not in the 10.2 SDK. So, call it via objc_msgSend_stret.
232-
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersionString)]) {
233-
NSString* (*procInfoFn)(id rec, SEL sel) = (NSString* (*)(id, SEL))objc_msgSend;
234-
NSString *nsVerStr = procInfoFn([NSProcessInfo processInfo],
235-
@selector(operatingSystemVersionString));
236-
if (nsVerStr != NULL) {
237-
// Copy out the char*
238-
osVersionCStr = strdup([nsVerStr UTF8String]);
238+
// Mac OS 10.9 includes the [NSProcessInfo operatingSystemVersion] function,
239+
// but it's not in the 10.9 SDK. So, call it via NSInvocation.
240+
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)]) {
241+
OSVerStruct osVer;
242+
NSMethodSignature *sig = [[NSProcessInfo processInfo] methodSignatureForSelector:
243+
@selector(operatingSystemVersion)];
244+
NSInvocation *invoke = [NSInvocation invocationWithMethodSignature:sig];
245+
invoke.selector = @selector(operatingSystemVersion);
246+
[invoke invokeWithTarget:[NSProcessInfo processInfo]];
247+
[invoke getReturnValue:&osVer];
248+
249+
NSString *nsVerStr;
250+
if (osVer.patchVersion == 0) { // Omit trailing ".0"
251+
nsVerStr = [NSString stringWithFormat:@"%ld.%ld",
252+
(long)osVer.majorVersion, (long)osVer.minorVersion];
253+
} else {
254+
nsVerStr = [NSString stringWithFormat:@"%ld.%ld.%ld",
255+
(long)osVer.majorVersion, (long)osVer.minorVersion, (long)osVer.patchVersion];
239256
}
257+
// Copy out the char*
258+
osVersionCStr = strdup([nsVerStr UTF8String]);
240259
}
241-
// Fallback if running on pre-10.2 Mac OS
260+
// Fallback if running on pre-10.9 Mac OS
242261
if (osVersionCStr == NULL) {
243262
NSDictionary *version = [NSDictionary dictionaryWithContentsOfFile :
244263
@"/System/Library/CoreServices/SystemVersion.plist"];

0 commit comments

Comments
 (0)