Skip to content

Luau segmentation fault on FromLuaConversionError #674

@kaathewisegit

Description

@kaathewisegit

Using mlua with Luau causes a segmentation fault when FromLua implementation of UserDataRef fails. Minimal reproducible example:

use mlua::{Lua, Result, UserData, UserDataMethods, UserDataRef};

fn main() -> Result<()> {
    let lua = Lua::new();

    struct MyUserData(i32);

    impl UserData for MyUserData {
        fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
            methods.add_method_mut("add", |_, this, other: UserDataRef<MyUserData>| {
                this.0 += other.0;
                Ok(())
            });
        }
    }

    lua.globals().set("myobject", MyUserData(1))?;

    lua.load("myobject:add(2)").exec()?;

    Ok(())
}

This returns a proper error with vendored Lua 5.1:

$ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/tmp`
Error: CallbackError { traceback: "stack traceback:\n\t[C]: in method 'add'\n\tsrc/main.rs:19:1: in main chunk", cause: BadArgument { to: Some("MyUserData.add"), pos: 2, name: None, cause: FromLuaConversionError { from: "number", to: "userdata", message: Some("expected userdata of type 'MyUserData'") } } }

But terminates with a segfault when luau is enabled:

cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/tmp`
Error: CallbackError { traceback: "stack traceback:\n\t[C]: in ?\n\tsrc/main.rs:19:1: in function <src/main.rs:19:1>", cause: BadArgument { to: Some("MyUserData.add"), pos: 2, name: None, cause: FromLuaConversionError { from: "Error: nu::shell::terminated_by_signal

  × External command was terminated by a signal
   ╭─[entry #55:1:1]
 1 │ cargo run
   · ──┬──
   ·   ╰── terminated by SIGSEGV (11)
   ╰────

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions