~matthiasbeyer/imag

9f2a67841a4ca37b0c95e5eb2b6558a6981785a6 — Matthias Beyer 1 year, 3 months ago 708c7b2
Fix: Remove Box::leak() call in runtime application abstraction

This patch removes the leakage in the runtime application abstraction.
Also fix formatting.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
1 files changed, 6 insertions(+), 16 deletions(-)

M lib/core/libimagrt/src/application.rs
M lib/core/libimagrt/src/application.rs => lib/core/libimagrt/src/application.rs +6 -16
@@ 37,34 37,24 @@ macro_rules! simple_imag_application_binary {
        extern crate failure;
        extern crate $application_library;

        use failure::{Error, Fallible as Result};
        
        use failure::{Error, Fallible as Result, ResultExt};

        fn main() {
            use libimagerror::trace::MapErrTrace;
            use libimagrt::application::ImagApplication;
            use libimagrt::setup::generate_runtime_setup;
            use $application_library::$application_implementor;
            

            let version = make_imag_version!();
            let rt = generate_runtime_setup($application_implementor::name(),
                                            &version,
                                            $application_implementor::description(),
                                            $application_implementor::build_cli);

            // The error context must have a 'static lifetime
            // Therefore, the easiest, safe, but hacky way to achieve this
            // is to allocate a string, which is then forgotten to
            // leak memory and return it's contents as a &'static str
            // Because this is the very end of the application and only
            // happens once, it should have no impact whatsoever
            let error_context: &'static str = Box::leak(
                format!("Failed to run {}", $application_implementor::name())
                    .into_boxed_str()
            );
            $application_implementor::run(rt)
                .map_err(|e| e.context(error_context))
		.map_err(Error::from)
		.map_err_trace_exit_unwrap();
                .context(format!("Failed to run {}", $application_implementor::name()))
                .map_err(Error::from)
                .map_err_trace_exit_unwrap();
        }
    };
}