Skip to content

Commit

Permalink
Fix luau_load segfault on null pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
bjcscat committed Dec 16, 2024
1 parent dd2eda0 commit 632ff6d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,27 @@ mod tests {
assert_eq!(load_result, Ok(()));
}

#[test]
fn cloned_compiler() {
let compiler = {
let original_compiler = Compiler::new();
original_compiler.clone()
};

// has an effect so cant be optimized out entirely
let result = compiler.compile("v()");

assert!(result.is_ok(), "Expected result to be a success");
assert!(
result.bytecode().is_some(),
"Expected resultant bytecode to be some"
);
assert!(
result.bytecode().is_some_and(|v| !v.is_empty()),
"Expected resultant bytecode to be non-empty"
);
}

#[test]
fn compiler_error() {
let compiler = Compiler::new();
Expand Down
24 changes: 23 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ impl Luau {
let success = unsafe {
luau_load(
self.state,
chunk_name.map(CStr::as_ptr).unwrap_or(null()),
chunk_name.unwrap_or(c"").as_ptr(),
bytecode.as_ptr() as _,
bytecode.len(),
env,
Expand Down Expand Up @@ -1327,6 +1327,10 @@ mod tests {

assert!(load_result.is_ok(), "Load result should be Ok");

let load_result = luau.load(Some(c"test"), result.bytecode().unwrap(), 0);

assert!(load_result.is_ok(), "Load result should be Ok");

luau.codegen(-1);

luau.call(0, 0);
Expand Down Expand Up @@ -1590,6 +1594,24 @@ mod tests {
matches!(status, LuauStatus::LUA_ERRRUN),
"Expected there to be a runtime error."
);

luau.push_function(
|l| {
l.check_args(1, None);

0
},
Some(c"test"),
0,
);

let status = luau.call(0, 0);

assert!(
matches!(status, LuauStatus::LUA_ERRRUN),
"Expected there to be a runtime error."
);

}

#[test]
Expand Down

0 comments on commit 632ff6d

Please sign in to comment.