Skip to content

Commit 60ef5af

Browse files
authored
Add missing availability checks for SecCopyErrorMessageString (#7577)
This requires iOS 11.3 and we currently target iOS 11.
1 parent 9e86d1f commit 60ef5af

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/realm/object-store/impl/apple/keychain_helper.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,21 @@ REALM_NORETURN
3636
REALM_COLD
3737
void keychain_access_exception(int32_t error_code)
3838
{
39-
if (auto message = adoptCF(SecCopyErrorMessageString(error_code, nullptr))) {
40-
if (auto msg = CFStringGetCStringPtr(message.get(), kCFStringEncodingUTF8)) {
41-
throw RuntimeError(ErrorCodes::RuntimeError,
42-
util::format("Keychain returned unexpected status code: %1 (%2)", msg, error_code));
43-
}
44-
auto length = CFStringGetMaximumSizeForEncoding(CFStringGetLength(message.get()), kCFStringEncodingUTF8) + 1;
45-
auto buffer = std::make_unique<char[]>(length);
46-
if (CFStringGetCString(message.get(), buffer.get(), length, kCFStringEncodingUTF8)) {
47-
throw RuntimeError(
48-
ErrorCodes::RuntimeError,
49-
util::format("Keychain returned unexpected status code: %1 (%2)", buffer.get(), error_code));
39+
if (__builtin_available(iOS 11.3, macOS 10.3, tvOS 11.3, watchOS 4.3, *)) {
40+
if (auto message = adoptCF(SecCopyErrorMessageString(error_code, nullptr))) {
41+
if (auto msg = CFStringGetCStringPtr(message.get(), kCFStringEncodingUTF8)) {
42+
throw RuntimeError(
43+
ErrorCodes::RuntimeError,
44+
util::format("Keychain returned unexpected status code: %1 (%2)", msg, error_code));
45+
}
46+
auto length =
47+
CFStringGetMaximumSizeForEncoding(CFStringGetLength(message.get()), kCFStringEncodingUTF8) + 1;
48+
auto buffer = std::make_unique<char[]>(length);
49+
if (CFStringGetCString(message.get(), buffer.get(), length, kCFStringEncodingUTF8)) {
50+
throw RuntimeError(
51+
ErrorCodes::RuntimeError,
52+
util::format("Keychain returned unexpected status code: %1 (%2)", buffer.get(), error_code));
53+
}
5054
}
5155
}
5256
throw RuntimeError(ErrorCodes::RuntimeError,

src/realm/sync/network/network_ssl.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,13 @@ std::string SecureTransportErrorCategory::message(int value) const
206206
{
207207
const char* message = "Unknown error";
208208
#if REALM_HAVE_SECURE_TRANSPORT
209-
auto status = OSStatus(value);
210-
void* reserved = nullptr;
211-
std::unique_ptr<char[]> buffer;
212-
if (auto cf_message = adoptCF(SecCopyErrorMessageString(status, reserved)))
213-
message = cfstring_to_cstring(cf_message.get(), buffer);
209+
if (__builtin_available(iOS 11.3, macOS 10.3, tvOS 11.3, watchOS 4.3, *)) {
210+
auto status = OSStatus(value);
211+
void* reserved = nullptr;
212+
std::unique_ptr<char[]> buffer;
213+
if (auto cf_message = adoptCF(SecCopyErrorMessageString(status, reserved)))
214+
message = cfstring_to_cstring(cf_message.get(), buffer);
215+
}
214216
#endif // REALM_HAVE_SECURE_TRANSPORT
215217

216218
return util::format("SecureTransport error: %1 (%2)", message, value); // Throws

0 commit comments

Comments
 (0)