Skip to content
This repository was archived by the owner on Sep 12, 2025. It is now read-only.

Commit 33ad968

Browse files
committed
Also remove the C searchers/loaders when removing loadlib.
1 parent 35a2c52 commit 33ad968

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/lua.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use crate::hook::{hook_proc, Debug, HookTriggers};
1919
use crate::markers::NoRefUnwindSafe;
2020
use crate::types::Callback;
2121
use crate::util::{
22-
assert_stack, dostring, init_error_registry, protect_lua_closure, push_globaltable, requiref,
23-
safe_pcall, safe_xpcall, userdata_destructor,
22+
assert_stack, dostring, init_error_registry, protect_lua_closure, push_globaltable, rawlen,
23+
requiref, safe_pcall, safe_xpcall, userdata_destructor,
2424
};
2525

2626
bitflags! {
@@ -713,9 +713,25 @@ unsafe fn create_lua(lua_mod_to_load: StdLib, init_flags: InitFlags) -> Lua {
713713
ffi::lua_getglobal(state, cstr!("package"));
714714
let t = ffi::lua_type(state, -1);
715715
if t == ffi::LUA_TTABLE {
716-
// Package is loaded
716+
// Package is loaded. Remove loadlib.
717717
ffi::lua_pushnil(state);
718718
ffi::lua_setfield(state, -2, cstr!("loadlib"));
719+
720+
#[cfg(rlua_lua51)]
721+
let searchers_name = cstr!("loaders");
722+
#[cfg(any(rlua_lua53, rlua_lua54))]
723+
let searchers_name = cstr!("searchers");
724+
725+
ffi::lua_getfield(state, -1, searchers_name);
726+
debug_assert_eq!(ffi::lua_type(state, -1), ffi::LUA_TTABLE);
727+
debug_assert_eq!(rawlen(state, -1), 4);
728+
// Remove the searchers/loaders which will load C libraries.
729+
ffi::lua_pushnil(state);
730+
ffi::lua_seti(state, -2, 4);
731+
ffi::lua_pushnil(state);
732+
ffi::lua_seti(state, -2, 3);
733+
734+
ffi::lua_pop(state, 1);
719735
} else {
720736
// Assume it's not present otherwise.
721737
assert_eq!(t, ffi::LUA_TNIL);

tests/tests.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,14 @@ fn test_default_loadlib() {
872872
let package = globals.get::<_, Table>("package").unwrap();
873873
let loadlib = package.get::<_, Function>("loadlib");
874874
assert!(loadlib.is_err());
875+
876+
lua.load(
877+
r#"
878+
assert(#(package.loaders or package.searchers) == 2)
879+
"#,
880+
)
881+
.exec()
882+
.unwrap();
875883
});
876884
}
877885

@@ -886,6 +894,14 @@ fn test_no_remove_loadlib() {
886894
let globals = lua.globals();
887895
let package = globals.get::<_, Table>("package").unwrap();
888896
let _loadlib = package.get::<_, Function>("loadlib").unwrap();
897+
898+
lua.load(
899+
r#"
900+
assert(#(package.loaders or package.searchers) == 4)
901+
"#,
902+
)
903+
.exec()
904+
.unwrap();
889905
});
890906
}
891907
}

0 commit comments

Comments
 (0)