5

How can I hide project info (like source code paths) from panic messages occurred while running a release build?

Assume following code as a Minima Reproducible Example

fn main() {
    println!("Hello!");
    let v = vec![0];
    println!("{:?}", v[1]); // panic - index out of bounds
}

> cargo build --release
> target/release/hello-rust
Getting:

Hello!
thread 'main' panicked at 'index out of bounds: the len is 1 but the index is 1', src/main.rs:5:22
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Revealing "src/main.rs:5:22" is very unwanted. I would prefer not to reveal even that the executable build with Rust.
I would like just to exit the process with "1" error code on every panic situation, like index out of bounds or .unwrap() on error.

Tried:

[profile.release]
panic = "abort"

Didn't help.
Tried to look in Profile Settings, didn't find something useful.

Shimon S
  • 4,048
  • 2
  • 29
  • 34
  • Seems, this could be a good solution https://doc.rust-lang.org/std/panic/fn.set_hook.html – Shimon S Jan 14 '22 at 10:18
  • 2
    If you merely change the panic hook at runtime, the executable will still contain all the information. – mcarton Jan 14 '22 at 10:21
  • @mcarton Good point. Is there a way to remove also from the executable? – Shimon S Jan 14 '22 at 10:23
  • strip all debug symbol I guess – Stargateur Jan 14 '22 at 10:26
  • You want `strip`. The rustc option got recently [stabilized](https://github.com/tmandry/rust/blob/0e13d0c20e8f64605c13c7ed6a086789e4de25cd/RELEASES.md#compiler), but using it from cargo still requires nightly(?). You can also just invoke the `strip` command on the executable from your shell. But I doubt you'll hide that the executable was made with rust. – Caesar Jan 14 '22 at 10:27
  • 1
    Stripping hides most things, but one only needs to run `strings theexecutable` to realize it was written in Rust. I don't think it will possible to completely hide this, no matter what. If someone really wants to know, they will. – mcarton Jan 14 '22 at 10:32
  • @ShimonS: Out of curiosity, why would you want this? – Caesar Jan 14 '22 at 11:23
  • 1
    @Caesar Regarding to original question (about console messages) I suppose it's obvious. Regarding the question in comments (about the executable) - just wondering. – Shimon S Jan 14 '22 at 12:26
  • This is such a valid question and it's amazing how nobody cares about this. Have you ever found any solution to this? – m4heshd Jul 08 '23 at 09:20

0 Answers0