如何检测 Rust 产生的命令是否导致分段错误?

发布于 2025-01-20 03:50:27 字数 577 浏览 3 评论 0原文

我正在努力构建一个包装器,以测试偶尔导致细分故障的应用程序。

use std::io::{Write};
use std::process::{Command, Stdio};

fn main() {

  let input_name = "Saqib Ali";
  let input_email = "[email protected]"
  let input_serial_numer = "SN103213112"
  let register_app = Command::new("register_drive").stdin(Stdio::piped()).spawn().unwrap();
  write!(register_app.stdin.unwrap(), "{}", input_name).unwrap();

}

input_name发送到它时,如何检测程序是否导致细分故障?

I am working to build a wrapper to test an application that occasionally results in a Segmentation Fault.

use std::io::{Write};
use std::process::{Command, Stdio};

fn main() {

  let input_name = "Saqib Ali";
  let input_email = "[email protected]"
  let input_serial_numer = "SN103213112"
  let register_app = Command::new("register_drive").stdin(Stdio::piped()).spawn().unwrap();
  write!(register_app.stdin.unwrap(), "{}", input_name).unwrap();

}

How do I detect if a program resulted in Segmentation Fault when a input_name was sent to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯℡ 2025-01-27 03:50:27

您可以等待该过程完成,然后使用 Unix 特定的 ExitStatusExt 特征从退出状态中提取信号代码,并使用 libc 包将信号标识为 <代码>SIGSEGV:

use std::os::unix::process::ExitStatusExt;

let input_name = "Saqib Ali";
let mut register_app = Command::new("register_drive")
    .stdin(Stdio::piped())
    .spawn()
    .unwrap();
write!(register_app.stdin.as_mut().unwrap(), "{}", input_name).unwrap();
let status = register_app.wait().unwrap();
match status.signal() {
    Some(signal) if signal == libc::SIGSEGV => {
        println!("the process resulted in segmentation fault");
    }
    _ => println!("the process resulted in {:?}", status),
}

Playground

请注意,您可能想要使用 writeln!() 写入程序的标准输入,否则具有输入名称的行不会终止,并且可能会与后续输入合并。

You can wait for the process to finish and use the Unix-specific ExitStatusExt trait to extract the signal code out of the exit status and the libc crate to identify the signal as SIGSEGV:

use std::os::unix::process::ExitStatusExt;

let input_name = "Saqib Ali";
let mut register_app = Command::new("register_drive")
    .stdin(Stdio::piped())
    .spawn()
    .unwrap();
write!(register_app.stdin.as_mut().unwrap(), "{}", input_name).unwrap();
let status = register_app.wait().unwrap();
match status.signal() {
    Some(signal) if signal == libc::SIGSEGV => {
        println!("the process resulted in segmentation fault");
    }
    _ => println!("the process resulted in {:?}", status),
}

Playground

Note that you might want to use writeln!() to write to the program's stdin, otherwise the line with the input name won't be terminated and might get merged with subsequent input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文