Skip to content

Commit

Permalink
fix(auth): Crash with EXC_BAD_ACCESS
Browse files Browse the repository at this point in the history
Adds checks to results of `IOServiceGetMatchingService` and `IORegistryEntryCreateCFProperty` FFI calls to prevent trying to access bad data in cast or free calls.
  • Loading branch information
Dillon Nys committed Aug 31, 2023
1 parent fca9d4d commit 8adf598
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/auth/amplify_auth_cognito_dart/ffigen.macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ enums:
macros:
include:
- kIOPlatformUUIDKey
- IO_OBJECT_NULL
typedefs:
include:
- CFTypeRef
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ final class ASFDeviceInfoMacOS extends ASFDeviceInfoPlatform {
Future<String?> get deviceOsReleaseVersion async => 'macOS/$_osVersion';

NSScreen? get _mainScreen {
try {
// NSScreen is not linked in Dart-only apps.
return NSScreen.getMainScreen(_native);
} on Exception {
// NSScreen is not linked in Dart-only apps.
if (!zIsFlutter) {
return null;
}
// TODO(dnys1): https://github.com/dart-lang/ffigen/issues/608
// return NSScreen.getMainScreen(_native);
return null;
}

@override
Expand All @@ -110,19 +111,28 @@ final class ASFDeviceInfoMacOS extends ASFDeviceInfoPlatform {
_mainScreen?.visibleFrame.size.width.toInt() ?? 0;

String? _ioValueFor<CFType>(String key) => using((arena) {
const serviceName = 'IOPlatformExpertDevice';
final service = _native.IOServiceGetMatchingService(
_native.kIOMasterPortDefault,
_native.IOServiceMatching(
'IOPlatformExpertDevice'.toNativeUtf8(allocator: arena).cast(),
serviceName.toNativeUtf8(allocator: arena).cast(),
),
);
logger.verbose('Found service matching $serviceName: $service');
if (service == IO_OBJECT_NULL) {
return null;
}
arena.onReleaseAll(() => _native.IOObjectRelease(service));
final data = _native.IORegistryEntryCreateCFProperty(
service,
key.toCFString(lib: _native, arena: arena).cast(),
_native.kCFAllocatorDefault,
0,
);
logger.verbose('Got data for $key: $data');
if (data == nullptr) {
return null;
}
arena.onReleaseAll(() => _native.CFRelease(data));
return switch (CFType) {
const (CFData) => data.cast<CFData>().toDartString(_native),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8adf598

Please sign in to comment.