on master help sign in
fix(dap): validate key and index exist when requesting vars (#5628)

Check if the stack frames contain the thread id and the frame before trying to get the frame id. If case any of the two fails to be found, provide the user with messages to inform them of the issue and gracefully return. Closes: #5625 Signed-off-by: Filip Dutescu <filip.dutescu@gmail.com>

commit44729fbaf9778b67869b2ed6b76b16b2354cc030 authorFilip Dutescu <filip.dutescu@gmail.com> dateMon 20 Feb 2023, 06:00 (4 years ago) parente3765ac
1 file changed+20−3
helix-term/src/commands/dap.rs+20−3
helix-term/src/commands/dap.rs+20−3
@@ -475,19 +475,36 @@ pub fn dap_variables(cx: &mut Context) {
475475
476476 if debugger.thread_id.is_none() {
477477 cx.editor
478− .set_status("Cannot access variables while target is running");
478+ .set_status("Cannot access variables while target is running.");
479479 return;
480480 }
481481 let (frame, thread_id) = match (debugger.active_frame, debugger.thread_id) {
482482 (Some(frame), Some(thread_id)) => (frame, thread_id),
483483 _ => {
484484 cx.editor
485− .set_status("Cannot find current stack frame to access variables");
485+ .set_status("Cannot find current stack frame to access variables.");
486486 return;
487487 }
488488 };
489489
490− let frame_id = debugger.stack_frames[&thread_id][frame].id;
490+ let thread_frame = match debugger.stack_frames.get(&thread_id) {
491+ Some(thread_frame) => thread_frame,
492+ None => {
493+ cx.editor
494+ .set_error("Failed to get stack frame for thread: {thread_id}");
495+ return;
496+ }
497+ };
498+ let stack_frame = match thread_frame.get(frame) {
499+ Some(stack_frame) => stack_frame,
500+ None => {
501+ cx.editor
502+ .set_error("Failed to get stack frame for thread {thread_id} and frame {frame}.");
503+ return;
504+ }
505+ };
506+
507+ let frame_id = stack_frame.id;
491508 let scopes = match block_on(debugger.scopes(frame_id)) {
492509 Ok(s) => s,
493510 Err(e) => {