From ceb740f709a1490c6a2fffe1c42b0e743232dbba Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 13 Mar 2016 19:15:16 -0400 Subject: [PATCH] Creating an IOServiceIterator could have a 0 value, confused for failure. Instead, return bool and use an out pointer for the iterator. --- apple-ir-control.cc | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/apple-ir-control.cc b/apple-ir-control.cc index 337a457..a48d7e1 100644 --- a/apple-ir-control.cc +++ b/apple-ir-control.cc @@ -111,16 +111,15 @@ bool SynchronizePrefs() { return rv; } -io_iterator_t CreateIOServiceIterator() { +bool CreateIOServiceIterator(io_iterator_t* iterator) { CFMutableDictionaryRef matching_dict = IOServiceMatching("AppleIRController"); - io_iterator_t iterator; kern_return_t kr = IOServiceGetMatchingServices( - kIOMasterPortDefault, matching_dict, &iterator); + kIOMasterPortDefault, matching_dict, iterator); if (kr != KERN_SUCCESS) { ERROR("Failed to IOServiceGetMatchingServices: 0x%x", kr); - return 0; + return false; } - return iterator; + return true;; } int HandleRead() { @@ -128,8 +127,8 @@ int HandleRead() { kCFPreferencesAnyUser, kCFPreferencesCurrentHost); printf("Userspace property value: %s\n", GetBooleanDescription(user_prop)); - io_iterator_t iterator = CreateIOServiceIterator(); - if (!iterator) + io_iterator_t iterator; + if (!CreateIOServiceIterator(&iterator)) return EXIT_FAILURE; io_object_t service; @@ -153,7 +152,7 @@ int HandleRead() { } if (!did_find) { - ERROR("Failed to match AppleIRController"); + ERROR("Failed to match any AppleIRController"); return EXIT_FAILURE; } @@ -173,7 +172,10 @@ int HandleWrite(bool enable) { if (!SynchronizePrefs()) return EXIT_FAILURE; - io_iterator_t iterator = CreateIOServiceIterator(); + io_iterator_t iterator; + if (!CreateIOServiceIterator(&iterator)) + return EXIT_FAILURE; + io_object_t service; while ((service = IOIteratorNext(iterator))) { io_name_t name; -- 2.43.5