Fix userdata memleak in edge case.

This can happen if we unable to push metatable with `__gc` metamethod after pushing userdata.
In this case Lua will never execute drop.
Instead, we will push metatable first and then userdata.
This commit is contained in:
Alex Orlenko
2021-06-18 17:45:20 +01:00
parent bf286751fa
commit 4e92ea341b
2 changed files with 11 additions and 5 deletions
+4 -1
View File
@@ -1977,8 +1977,11 @@ impl Lua {
let _sg = StackGuard::new(self.state);
check_stack(self.state, 2)?;
push_userdata(self.state, data)?;
// If we unable to push metatable, then we should not push userdata.
// Otherwise we can have a memory leak.
self.push_userdata_metatable::<T>()?;
push_userdata(self.state, data)?;
ffi::lua_rotate(self.state, -2, 1);
ffi::lua_setmetatable(self.state, -2);
Ok(AnyUserData(self.pop_ref()))