radareorg / r2pipe.rs

Rust crate for r2pipe
Other
45 stars 19 forks source link

Simplify the cmd method #43

Closed RHL120 closed 2 years ago

RHL120 commented 2 years ago

Checklist

Description

meme commented 2 years ago

This causes infinite recursion:

struct A;
impl A {
  fn cmd(&mut self, _cmd: &str) -> Option<usize> { println!("A"); Some(1) }
}

struct B;
impl B {
  fn cmd(&mut self, _cmd: &str) -> Option<usize> { println!("B"); Some(2) }
}

enum E {
    A(A),
    B(B),
}

impl E {
  fn cmd(&mut self, cmd: &str) -> Option<usize> {
    self.cmd(cmd.trim())
  }
}

fn main() {
  let a = A {};
  let mut e = E::A(a);
  e.cmd("aa"); // Blows up the stack
}

Without a call to .trim() the compiler warns us:

warning: function cannot return without recursing
  [--> src/main.rs:17:3
](https://play.rust-lang.org/#)   |
17 |   fn cmd(&mut self, cmd: &str) -> Option<usize> {
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
...
22 |     self.cmd(cmd)
   |     ------------- recursive call site
   |
   = note: `#[warn(unconditional_recursion)]` on by default
   = help: a `loop` may express intention better if this is on purpose