Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Coverity] Fix an unchecked dynamic cast issues #12485

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ds/d400/d400-auto-calibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,9 @@ namespace librealsense
if (_preset_change)
{
auto advanced_mode = dynamic_cast<ds_advanced_mode_base*>(this);
if (!advanced_mode)
throw std::runtime_error("Can not cast to advance mode base");

if (_old_preset == RS2_RS400_VISUAL_PRESET_CUSTOM)
{
advanced_mode->_preset_opt->set(RS2_RS400_VISUAL_PRESET_CUSTOM);
Expand Down
2 changes: 2 additions & 0 deletions src/ds/ds-motion-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ namespace librealsense
assign_stream(fisheye_stream, p);

auto video = dynamic_cast<video_stream_profile_interface*>(p.get());
if (!video)
throw std::runtime_error("Stream profile interface is not video stream profile interface");

auto profile = to_profile(p.get());
std::weak_ptr<ds_fisheye_sensor> wp =
Expand Down
5 changes: 5 additions & 0 deletions src/gl/align-gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ void align_gl::align_z_to_other(rs2::video_frame& aligned,

// Set the depth origin of new depth frame to the old depth frame
auto depth_ptr = dynamic_cast<librealsense::depth_frame*>((librealsense::frame_interface*)depth.get());
if (!depth_ptr)
throw std::runtime_error("Frame interface is not depth frame");

frame_ref->set_sensor(depth_ptr->get_sensor());
depth_ptr->acquire();
frame_holder h{ depth_ptr };
Expand Down Expand Up @@ -238,6 +241,8 @@ void align_gl::align_other_to_z(rs2::video_frame& aligned,

auto frame_ref = (frame_interface*)aligned.get();
auto gf = dynamic_cast<gpu_addon_interface*>(frame_ref);
if (!gf)
throw std::runtime_error("Frame interface is not gpu addon interface");

uint32_t output_rgb;
auto format = other.get_profile().format();
Expand Down
3 changes: 3 additions & 0 deletions src/gl/colorizer-gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ namespace librealsense

auto fi = (frame_interface*)f.get();
auto df = dynamic_cast<librealsense::depth_frame*>(fi);
if (!df)
throw std::runtime_error("Frame interface is not depth frame");

_depth_units = df->get_units();
bool disparity = f.get_profile().format() == RS2_FORMAT_DISPARITY32 ? true : false;

Expand Down
2 changes: 2 additions & 0 deletions src/gl/pointcloud-gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ void pointcloud_gl::get_texture_map(
auto frame_ref = (frame_interface*)output.get();

auto gf = dynamic_cast<gpu_addon_interface*>(frame_ref);
if (!gf)
throw std::runtime_error("Frame interface is not gpu addon interface");

uint32_t depth_texture;

Expand Down
6 changes: 5 additions & 1 deletion src/gl/upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ namespace librealsense
if (new_f)
{
auto ptr = dynamic_cast<librealsense::depth_frame*>((librealsense::frame_interface*)new_f.get());
if (!ptr)
throw std::runtime_error("Frame is not depth frame");

auto orig = (librealsense::frame_interface*)f.get();
auto depth_data = (uint16_t*)orig->get_frame_data();
Expand All @@ -128,7 +130,9 @@ namespace librealsense
//scoped_timer t("upload.depth");

auto gf = dynamic_cast<gpu_addon_interface*>((frame_interface*)new_f.get());

if (!gf)
throw std::runtime_error("Frame interface is not gpu addon interface");

uint32_t depth_texture;
gf->get_gpu_section().output_texture(0, &depth_texture, TEXTYPE_UINT16);
glBindTexture(GL_TEXTURE_2D, depth_texture);
Expand Down
2 changes: 2 additions & 0 deletions src/gl/y4112rgb-gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ rs2::frame y411_2rgb::process_frame(const rs2::frame_source& src, const rs2::fra
if (!res) return;

auto gf = dynamic_cast<gpu_addon_interface*>((frame_interface*)res.get());
if (!gf)
throw std::runtime_error("Frame interface is not gpu addon interface");

uint32_t yuy_texture;

Expand Down
2 changes: 2 additions & 0 deletions src/gl/yuy2rgb-gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ rs2::frame yuy2rgb::process_frame(const rs2::frame_source& src, const rs2::frame
if (!res) return;

auto gf = dynamic_cast<gpu_addon_interface*>((frame_interface*)res.get());
if (!gf)
throw std::runtime_error("Frame interface is not gpu addon interface");

uint32_t yuy_texture;

Expand Down
3 changes: 2 additions & 1 deletion src/media/ros/ros_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ namespace librealsense
{
sensor_msgs::Image image;
auto vid_frame = dynamic_cast<librealsense::video_frame*>(frame.frame);
assert(vid_frame != nullptr);
if (!vid_frame)
throw std::runtime_error("Frame is not video frame");

image.width = static_cast<uint32_t>(vid_frame->get_width());
image.height = static_cast<uint32_t>(vid_frame->get_height());
Expand Down
9 changes: 9 additions & 0 deletions src/proc/decimation-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,13 @@ namespace librealsense
{
_target_stream_profile = pf->second;
auto tgt_vspi = dynamic_cast<video_stream_profile_interface*>(_target_stream_profile.get()->profile);
if (!tgt_vspi)
throw std::runtime_error("Stream profile interface is not video stream profile interface");

auto f_pf = dynamic_cast<video_stream_profile_interface*>(_source_stream_profile.get()->profile);
if (!f_pf)
throw std::runtime_error("Stream profile interface is not video stream profile interface");

rs2_intrinsics tgt_intrin = tgt_vspi->get_intrinsics();

// Update real/padded output frame size based on retrieved input properties
Expand All @@ -315,6 +321,9 @@ namespace librealsense

auto tmp_profile = _source_stream_profile.clone(_source_stream_profile.stream_type(), _source_stream_profile.stream_index(), _source_stream_profile.format());
auto src_vspi = dynamic_cast<video_stream_profile_interface*>(_source_stream_profile.get()->profile);
if (!src_vspi)
throw std::runtime_error("Stream profile interface is not video stream profile interface");

auto tgt_vspi = dynamic_cast<video_stream_profile_interface*>(tmp_profile.get()->profile);
rs2_intrinsics src_intrin = src_vspi->get_intrinsics();
rs2_intrinsics tgt_intrin = tgt_vspi->get_intrinsics();
Expand Down
7 changes: 7 additions & 0 deletions src/proc/disparity-transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,15 @@ namespace librealsense
{
auto tgt_format = _transform_to_disparity ? RS2_FORMAT_DISPARITY32 : RS2_FORMAT_Z16;
_target_stream_profile = _source_stream_profile.clone(RS2_STREAM_DEPTH, 0, tgt_format);

auto src_vspi = dynamic_cast<video_stream_profile_interface*>(_source_stream_profile.get()->profile);
if (!src_vspi)
throw std::runtime_error("Stream profile is not video stream profile");

auto tgt_vspi = dynamic_cast<video_stream_profile_interface*>(_target_stream_profile.get()->profile);
if (!tgt_vspi)
throw std::runtime_error("Stream profile is not video stream profile");

rs2_intrinsics src_intrin = src_vspi->get_intrinsics();

tgt_vspi->set_intrinsics([src_intrin]() { return src_intrin; });
Expand Down
5 changes: 5 additions & 0 deletions src/proc/hdr-merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ namespace librealsense
if (new_f)
{
auto ptr = dynamic_cast<librealsense::depth_frame*>((librealsense::frame_interface*)new_f.get());
if (!ptr)
throw std::runtime_error("Frame interface is not depth frame");

auto orig = dynamic_cast<librealsense::depth_frame*>((librealsense::frame_interface*)first_depth.get());
if (!orig)
throw std::runtime_error("Frame interface is not depth frame");

auto d0 = (uint16_t*)first_depth.get_data();
auto d1 = (uint16_t*)second_depth.get_data();
Expand Down
18 changes: 17 additions & 1 deletion src/proc/synthetic-stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,16 @@ namespace librealsense
}

auto of = dynamic_cast<frame*>(original);
if (!of)
throw std::runtime_error("Can not cast frame interface to frame");

frame_additional_data data = of->additional_data;
auto res = _actual_source.alloc_frame( frame_type, stride * height, std::move( data ), true );
if (!res) throw wrong_api_call_sequence_exception("Out of frame resources!");
vf = dynamic_cast<video_frame*>(res);
if (!vf)
throw std::runtime_error("Frame is not video frame");

vf->metadata_parsers = of->metadata_parsers;
vf->assign(width, height, stride, bpp);
vf->set_sensor(original->get_sensor());
Expand All @@ -420,7 +426,10 @@ namespace librealsense
if (frame_type == RS2_EXTENSION_DEPTH_FRAME)
{
original->acquire();
(dynamic_cast<depth_frame*>(res))->set_original(original);
auto frame = dynamic_cast<depth_frame*>(res);
if (!frame)
throw std::runtime_error("Frame interface is not depth frame");
frame->set_original(original);
}

return res;
Expand All @@ -431,10 +440,17 @@ namespace librealsense
rs2_extension frame_type)
{
auto of = dynamic_cast<frame*>(original);
if (!of)
throw std::runtime_error("Frame interface is not frame");

frame_additional_data data = of->additional_data;
auto res = _actual_source.alloc_frame( frame_type, of->get_frame_data_size(), std::move( data ), true );
if (!res) throw wrong_api_call_sequence_exception("Out of frame resources!");

auto mf = dynamic_cast<motion_frame*>(res);
if (!mf)
throw std::runtime_error("Frame interface is not motion frame");

mf->metadata_parsers = of->metadata_parsers;
mf->set_sensor(original->get_sensor());
res->set_stream(stream);
Expand Down
5 changes: 5 additions & 0 deletions src/proc/threshold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ namespace librealsense
if (new_f)
{
auto ptr = dynamic_cast<librealsense::depth_frame*>((librealsense::frame_interface*)new_f.get());
if (!ptr)
throw std::runtime_error("Frame is not depth frame");

auto orig = dynamic_cast<librealsense::depth_frame*>((librealsense::frame_interface*)f.get());
if (!orig)
throw std::runtime_error("Frame is not depth frame");

auto depth_data = (uint16_t*)orig->get_frame_data();
auto new_data = (uint16_t*)ptr->get_frame_data();
Expand Down
2 changes: 2 additions & 0 deletions src/rs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,8 @@ int rs2_processing_block_register_simple_option(rs2_processing_block* block, rs2
std::shared_ptr<option> opt = std::make_shared<float_option>(option_range{ min, max, step, def });
// TODO: am I supposed to use the extensions API here?
auto options = dynamic_cast<options_container*>(block->options);
if (!options)
throw std::runtime_error("Options are not container options");
options->register_option(option_id, opt);
return true;
}
Expand Down