From e0c153262860ad219d4d2895267a4dd7ad5ddcd7 Mon Sep 17 00:00:00 2001 From: Remi Bettan Date: Wed, 18 Dec 2024 14:05:53 +0200 Subject: [PATCH 1/5] improvements to avoid crashes --- src/ds/d400/d400-device.cpp | 11 ++++++++--- src/ds/d500/d500-device.cpp | 12 ++++++++---- src/win/win-helpers.cpp | 9 +++++---- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/ds/d400/d400-device.cpp b/src/ds/d400/d400-device.cpp index b390e9aac8..ec7bb53d3a 100644 --- a/src/ds/d400/d400-device.cpp +++ b/src/ds/d400/d400-device.cpp @@ -487,12 +487,17 @@ namespace librealsense std::vector> depth_devices; auto depth_devs_info = filter_by_mi( all_device_infos, 0 ); - if ( depth_devs_info.empty() ) + for (auto&& info : depth_devs_info) // Filter just mi=0, DEPTH + { + auto depth_uvc_device = get_backend()->create_uvc_device(info); + if (depth_uvc_device) + depth_devices.push_back(depth_uvc_device); + } + + if (depth_devs_info.empty() || depth_devices.empty()) { throw backend_exception("cannot access depth sensor", RS2_EXCEPTION_TYPE_BACKEND); } - for (auto&& info : depth_devs_info) // Filter just mi=0, DEPTH - depth_devices.push_back( get_backend()->create_uvc_device( info ) ); std::unique_ptr< frame_timestamp_reader > timestamp_reader_backup( new ds_timestamp_reader() ); frame_timestamp_reader* timestamp_reader_from_metadata; diff --git a/src/ds/d500/d500-device.cpp b/src/ds/d500/d500-device.cpp index e33c9b144c..3d78487694 100644 --- a/src/ds/d500/d500-device.cpp +++ b/src/ds/d500/d500-device.cpp @@ -359,13 +359,17 @@ namespace librealsense std::vector> depth_devices; auto depth_devs_info = filter_by_mi( all_device_infos, 0 ); - if ( depth_devs_info.empty() ) + for (auto&& info : depth_devs_info) // Filter just mi=0, DEPTH { - throw backend_exception("cannot access depth sensor", RS2_EXCEPTION_TYPE_BACKEND); + auto depth_uvc_device = get_backend()->create_uvc_device(info); + if (depth_uvc_device) + depth_devices.push_back(depth_uvc_device); } - for( auto & info : depth_devs_info ) // Filter just mi=0, DEPTH - depth_devices.push_back( get_backend()->create_uvc_device( info ) ); + if (depth_devs_info.empty() || depth_devices.empty()) + { + throw backend_exception("cannot access depth sensor", RS2_EXCEPTION_TYPE_BACKEND); + } std::unique_ptr< frame_timestamp_reader > timestamp_reader_backup( new ds_timestamp_reader() ); std::unique_ptr timestamp_reader_metadata(new ds_timestamp_reader_from_metadata(std::move(timestamp_reader_backup))); diff --git a/src/win/win-helpers.cpp b/src/win/win-helpers.cpp index 1c85a0d1f6..c95841118e 100644 --- a/src/win/win-helpers.cpp +++ b/src/win/win-helpers.cpp @@ -491,14 +491,15 @@ namespace librealsense } // Enumerate all imaging devices - for (int member_index = 0; ; ++member_index) + for (DWORD member_index = 0; member_index < 0x10000; ++member_index) { // Get device information element from the device information set if (SetupDiEnumDeviceInfo(device_info, member_index, &devInfo) == FALSE) { - if( GetLastError() == ERROR_NO_MORE_ITEMS ) - break; // stop when none left - continue; // silently ignore other errors + DWORD last_error = GetLastError(); + if ((last_error == ERROR_NO_MORE_ITEMS) || (last_error == ERROR_INVALID_HANDLE)) + break; // stop when none left + continue; // silently ignore other errors } } std::string parent_uid; From d89160e96fc1ac5d5966f93b10be4d9b4b52dd78 Mon Sep 17 00:00:00 2001 From: Remi Bettan Date: Sun, 22 Dec 2024 10:14:37 +0200 Subject: [PATCH 2/5] comment added --- src/win/win-helpers.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/win/win-helpers.cpp b/src/win/win-helpers.cpp index c95841118e..3a22da2191 100644 --- a/src/win/win-helpers.cpp +++ b/src/win/win-helpers.cpp @@ -491,6 +491,7 @@ namespace librealsense } // Enumerate all imaging devices + // high limit is below loop has been added to avoid infinite loop for (DWORD member_index = 0; member_index < 0x10000; ++member_index) { // Get device information element from the device information set From bd6a0aa520dbb28bfcbfed8f917162503f39d619 Mon Sep 17 00:00:00 2001 From: Remi Bettan Date: Tue, 31 Dec 2024 14:47:24 +0200 Subject: [PATCH 3/5] comment updated, branch rebased --- src/win/win-helpers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/win/win-helpers.cpp b/src/win/win-helpers.cpp index 3a22da2191..17ee6fc80f 100644 --- a/src/win/win-helpers.cpp +++ b/src/win/win-helpers.cpp @@ -491,7 +491,7 @@ namespace librealsense } // Enumerate all imaging devices - // high limit is below loop has been added to avoid infinite loop + // 0x10000 ia a large enough limit to permit maximum connection, while avoiding infinite loop for (DWORD member_index = 0; member_index < 0x10000; ++member_index) { // Get device information element from the device information set From 142918c32d8cd6dc8f52793fbd6625eef4550155 Mon Sep 17 00:00:00 2001 From: Remi Bettan Date: Sun, 5 Jan 2025 13:15:08 +0200 Subject: [PATCH 4/5] lowering the iterations max limit --- src/win/win-helpers.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/win/win-helpers.cpp b/src/win/win-helpers.cpp index 17ee6fc80f..702e688cff 100644 --- a/src/win/win-helpers.cpp +++ b/src/win/win-helpers.cpp @@ -491,8 +491,8 @@ namespace librealsense } // Enumerate all imaging devices - // 0x10000 ia a large enough limit to permit maximum connection, while avoiding infinite loop - for (DWORD member_index = 0; member_index < 0x10000; ++member_index) + // 0x1000 ia a large enough limit to permit maximum connection, while avoiding infinite loop + for (DWORD member_index = 0; member_index < 0x1000; ++member_index) { // Get device information element from the device information set if (SetupDiEnumDeviceInfo(device_info, member_index, &devInfo) == FALSE) From 75a27040d779f2f0d6b36e7aa18ee7ed34a79c14 Mon Sep 17 00:00:00 2001 From: Remi Bettan Date: Sun, 5 Jan 2025 13:16:41 +0200 Subject: [PATCH 5/5] not breaking the loop if ERROR_INVALID_HANDLE is received --- src/win/win-helpers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/win/win-helpers.cpp b/src/win/win-helpers.cpp index 702e688cff..b02b1d1fb7 100644 --- a/src/win/win-helpers.cpp +++ b/src/win/win-helpers.cpp @@ -498,7 +498,7 @@ namespace librealsense if (SetupDiEnumDeviceInfo(device_info, member_index, &devInfo) == FALSE) { DWORD last_error = GetLastError(); - if ((last_error == ERROR_NO_MORE_ITEMS) || (last_error == ERROR_INVALID_HANDLE)) + if (last_error == ERROR_NO_MORE_ITEMS) break; // stop when none left continue; // silently ignore other errors } }