From 6eb40ec005cfd440a99cc51df3e6699575b659ef Mon Sep 17 00:00:00 2001 From: NirAz Date: Mon, 29 Mar 2021 13:18:14 +0300 Subject: [PATCH 1/2] protect opengl inputs --- common/viewer.cpp | 31 ++++++++++++++------- tools/depth-quality/depth-quality-model.cpp | 23 ++++++++++++++- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/common/viewer.cpp b/common/viewer.cpp index a951213108..1401e2ac1c 100644 --- a/common/viewer.cpp +++ b/common/viewer.cpp @@ -24,6 +24,8 @@ #include "../common/utilities/string/trim-newlines.h" #include "../common/utilities/imgui/wrap.h" +#define FORCE_NON_NEGATIVE(input) input < 0 ? 0 : input + namespace rs2 { // Allocates a frameset from points and texture frames @@ -1392,9 +1394,6 @@ namespace rs2 error_message = ex.what(); } - - - window.begin_viewport(); auto flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove @@ -1410,11 +1409,23 @@ namespace rs2 ImGui::Begin("Viewport", nullptr, { viewer_rect.w, viewer_rect.h }, 0.f, flags); - draw_viewport(viewer_rect, window, devices, error_message, texture_frame, p); + try + { + draw_viewport( viewer_rect, window, devices, error_message, texture_frame, p ); - modal_notification_on = not_model->draw(window, - static_cast(window.width()), static_cast(window.height()), - error_message); + modal_notification_on = not_model->draw( window, + static_cast< int >( window.width() ), + static_cast< int >( window.height() ), + error_message ); + } + catch( const error & e ) + { + error_message = error_to_string( e ); + } + catch( const std::exception & e ) + { + error_message = e.what(); + } popup_if_error(window, error_message); @@ -1930,8 +1941,8 @@ namespace rs2 auto bottom_y = win.framebuf_height() - viewer_rect.y - viewer_rect.h; - glViewport(static_cast(viewer_rect.x), static_cast(bottom_y), - static_cast(viewer_rect.w), static_cast(viewer_rect.h - top_bar_height)); + glViewport(static_cast(FORCE_NON_NEGATIVE(viewer_rect.x)), static_cast(FORCE_NON_NEGATIVE(bottom_y)), + static_cast(FORCE_NON_NEGATIVE(viewer_rect.w)), static_cast(FORCE_NON_NEGATIVE(viewer_rect.h - top_bar_height))); glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -1940,7 +1951,7 @@ namespace rs2 glMatrixMode(GL_PROJECTION); glPushMatrix(); - gluPerspective(45, viewer_rect.w / win.framebuf_height(), 0.001f, 100.0f); + gluPerspective(45, FORCE_NON_NEGATIVE(viewer_rect.w / win.framebuf_height()), 0.001f, 100.0f); matrix4 perspective_mat; glGetFloatv(GL_PROJECTION_MATRIX, perspective_mat); glPopMatrix(); diff --git a/tools/depth-quality/depth-quality-model.cpp b/tools/depth-quality/depth-quality-model.cpp index 3e5ea418bf..3d0fcc1392 100644 --- a/tools/depth-quality/depth-quality-model.cpp +++ b/tools/depth-quality/depth-quality-model.cpp @@ -842,7 +842,28 @@ namespace rs2 if (dpt) _metrics_model.begin_process_frame(dpt); } - catch (...){} // on device disconnect + catch( const error & e ) + { + // Can occur on device disconnect + _viewer_model.not_model->output.add_log( RS2_LOG_SEVERITY_DEBUG, + __FILE__, + __LINE__, + error_to_string( e ) ); + } + catch( const std::exception & e ) + { + _viewer_model.not_model->output.add_log( RS2_LOG_SEVERITY_ERROR, + __FILE__, + __LINE__, + e.what() ); + } + catch( ... ) + { + _viewer_model.not_model->output.add_log( RS2_LOG_SEVERITY_ERROR, + __FILE__, + __LINE__, + "Unknown error occurred" ); + } } From a547cf283804aa7bb2886b827a6bd2b6077749d4 Mon Sep 17 00:00:00 2001 From: NirAz Date: Tue, 30 Mar 2021 10:11:00 +0300 Subject: [PATCH 2/2] replace macro with template --- common/viewer.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/common/viewer.cpp b/common/viewer.cpp index 1401e2ac1c..e572779334 100644 --- a/common/viewer.cpp +++ b/common/viewer.cpp @@ -24,10 +24,15 @@ #include "../common/utilities/string/trim-newlines.h" #include "../common/utilities/imgui/wrap.h" -#define FORCE_NON_NEGATIVE(input) input < 0 ? 0 : input namespace rs2 { + template + T non_negative(const T& input) + { + return std::max(static_cast(0), input); + } + // Allocates a frameset from points and texture frames frameset_allocator::frameset_allocator(viewer_model* viewer) : owner(viewer), filter([this](frame f, frame_source& s) @@ -1941,8 +1946,8 @@ namespace rs2 auto bottom_y = win.framebuf_height() - viewer_rect.y - viewer_rect.h; - glViewport(static_cast(FORCE_NON_NEGATIVE(viewer_rect.x)), static_cast(FORCE_NON_NEGATIVE(bottom_y)), - static_cast(FORCE_NON_NEGATIVE(viewer_rect.w)), static_cast(FORCE_NON_NEGATIVE(viewer_rect.h - top_bar_height))); + glViewport(static_cast(non_negative(viewer_rect.x)), static_cast(non_negative(bottom_y)), + static_cast(non_negative(viewer_rect.w)), static_cast(non_negative(viewer_rect.h - top_bar_height))); glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -1951,7 +1956,7 @@ namespace rs2 glMatrixMode(GL_PROJECTION); glPushMatrix(); - gluPerspective(45, FORCE_NON_NEGATIVE(viewer_rect.w / win.framebuf_height()), 0.001f, 100.0f); + gluPerspective(45, non_negative(viewer_rect.w / win.framebuf_height()), 0.001f, 100.0f); matrix4 perspective_mat; glGetFloatv(GL_PROJECTION_MATRIX, perspective_mat); glPopMatrix();