Skip to content

Commit

Permalink
Handle overflow when writing out registers
Browse files Browse the repository at this point in the history
  • Loading branch information
DrChat committed Jun 1, 2021
1 parent 37c82ec commit 6b19ed0
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/gdbstub_impl/ext/single_register_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,33 @@ impl<T: Target, C: Connection> GdbStubImpl<T, C> {
let handler_status = match command {
SingleRegisterAccess::p(p) => {
let reg = <T::Arch as Arch>::RegId::from_raw_id(p.reg_id);
let (reg_id, _reg_size) = match reg {
let (reg_id, reg_size) = match reg {
// empty packet indicates unrecognized query
None => return Ok(HandlerStatus::Handled),
Some(v) => v,
};

let mut n = 0usize;
let mut err = Ok(());

// TODO: Limit the number of bytes transferred on the wire to the register size
// (if specified). Maybe pad the register if the callee does not
// send enough data?
ops.read_register(
id,
reg_id,
SendRegisterOutput::new(&mut |buf| match res.write_hex_buf(buf) {
Ok(_) => {}
Err(e) => err = Err(e),
SendRegisterOutput::new(&mut |buf| {
if err.is_ok() {
// If the register has a known size and read_register attempts
// to send more bytes than are present in the register,
// error out and stop sending data.
n += buf.len();
if let Some(size) = reg_size {
if n > size.get() {
err = Err(Error::TargetMismatch);
return;
}
}

err = res.write_hex_buf(buf).map_err(|e| e.into());
}
}),
)
.handle_error()?;
Expand Down

0 comments on commit 6b19ed0

Please sign in to comment.