Skip to content

Commit 73b912f

Browse files
committed
Make all extern "C" functions unsafe
rust-lang/rust#23452 makes safe functions no longer a subtype of unsafe functions. For consistency, change the definitions of `raw::lua_Reader`, `raw::lua_Writer`, and `raw::lua_Alloc` to be `unsafe` and update all places where we provide these functions. Also update the `lua_extern!` and `lua_extern_pub!` macros to produce `unsafe` functions.
1 parent a01f1c0 commit 73b912f

File tree

5 files changed

+33
-42
lines changed

5 files changed

+33
-42
lines changed

src/lib.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -308,22 +308,18 @@ impl State {
308308
}
309309
};
310310

311-
extern "C" fn alloc(_ud: *mut libc::c_void, ptr: *mut libc::c_void, _osize: libc::size_t,
312-
nsize: libc::size_t) -> *mut libc::c_void {
313-
unsafe {
314-
if nsize == 0 {
315-
libc::free(ptr as *mut libc::c_void);
316-
ptr::null_mut()
317-
} else {
318-
libc::realloc(ptr, nsize)
319-
}
311+
unsafe extern "C" fn alloc(_ud: *mut libc::c_void, ptr: *mut libc::c_void,
312+
_osize: libc::size_t, nsize: libc::size_t) -> *mut libc::c_void {
313+
if nsize == 0 {
314+
libc::free(ptr as *mut libc::c_void);
315+
ptr::null_mut()
316+
} else {
317+
libc::realloc(ptr, nsize)
320318
}
321319
}
322-
extern "C" fn panic(L: *mut raw::lua_State) -> c_int {
323-
unsafe {
324-
let s = RawState::from_lua_State(L).describe_(-1, false);
325-
panic!("unprotected error in call to Lua API ({})", s);
326-
}
320+
unsafe extern "C" fn panic(L: *mut raw::lua_State) -> c_int {
321+
let s = RawState::from_lua_State(L).describe_(-1, false);
322+
panic!("unprotected error in call to Lua API ({})", s);
327323
}
328324
}
329325
}

src/macro.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
macro_rules! lua_extern {
55
($(unsafe fn $name:ident($arg:ident: &mut $typ:ty) -> i32 $code:block)+) => (
66
$(
7-
extern "C" fn $name($arg: *mut ::lua::raw::lua_State) -> ::libc::c_int {
8-
unsafe {
9-
let mut $arg = ::lua::ExternState::from_lua_State($arg);
10-
return inner(&mut $arg) as ::libc::c_int;
11-
}
7+
unsafe extern "C" fn $name($arg: *mut ::lua::raw::lua_State) -> ::libc::c_int {
8+
let mut $arg = ::lua::ExternState::from_lua_State($arg);
9+
return inner(&mut $arg) as ::libc::c_int;
1210

1311
unsafe fn inner($arg: &mut $typ) -> i32 $code
1412
}
@@ -20,11 +18,9 @@ macro_rules! lua_extern {
2018
macro_rules! lua_extern_pub {
2119
($(unsafe fn $name:ident($arg:ident: &mut $typ:ty) -> i32 $code:block)+) => (
2220
$(
23-
pub extern "C" fn $name($arg: *mut ::lua::raw::lua_State) -> ::libc::c_int {
24-
unsafe {
25-
let mut $arg = ::lua::ExternState::from_lua_State($arg);
26-
return inner(&mut $arg) as ::libc::c_int;
27-
}
21+
pub unsafe extern "C" fn $name($arg: *mut ::lua::raw::lua_State) -> ::libc::c_int {
22+
let mut $arg = ::lua::ExternState::from_lua_State($arg);
23+
return inner(&mut $arg) as ::libc::c_int;
2824

2925
unsafe fn inner($arg: &mut $typ) -> i32 $code
3026
}

src/raw.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ pub type lua_State = libc::c_void;
4646
pub type lua_CFunction = unsafe extern "C" fn(L: *mut lua_State) -> c_int;
4747

4848
/// Function type for reading blocks when loading Lua chunks.
49-
pub type lua_Reader = extern "C" fn(L: *mut lua_State, ud: *mut libc::c_void,
50-
sz: *mut libc::size_t) -> *const libc::c_char;
49+
pub type lua_Reader = unsafe extern "C" fn(L: *mut lua_State, ud: *mut libc::c_void,
50+
sz: *mut libc::size_t) -> *const libc::c_char;
5151
/// Function type for writing blocks when dumping Lua chunks.
52-
pub type lua_Writer = extern "C" fn(L: *mut lua_State, p: *const libc::c_void, sz: libc::size_t,
53-
ud: *mut libc::c_void) -> libc::c_int;
52+
pub type lua_Writer = unsafe extern "C" fn(L: *mut lua_State, p: *const libc::c_void, sz: libc::size_t,
53+
ud: *mut libc::c_void) -> libc::c_int;
5454

5555
/// Prototype for memory-allocation functions
56-
pub type lua_Alloc = extern "C" fn(ud: *mut libc::c_void, ptr: *mut libc::c_void,
57-
osize: libc::size_t, nsize: libc::size_t) -> *mut libc::c_void;
56+
pub type lua_Alloc = unsafe extern "C" fn(ud: *mut libc::c_void, ptr: *mut libc::c_void,
57+
osize: libc::size_t, nsize: libc::size_t)
58+
-> *mut libc::c_void;
5859

5960
// lua_State manipulation
6061
extern {

src/rawtests.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@ use aux;
66

77
// implement the same function that luaL_newstate uses, so we can test lua_newstate directly
88
// FIXME (#10025): We can't define this as `unsafe extern "C"`
9-
extern "C" fn alloc_helper(_ud: *mut libc::c_void, ptr: *mut libc::c_void, _osize: libc::size_t,
10-
nsize: libc::size_t) -> *mut libc::c_void {
11-
unsafe {
12-
if nsize == 0 {
13-
libc::free(ptr as *mut libc::c_void);
14-
ptr::null_mut()
15-
} else {
16-
libc::realloc(ptr, nsize)
17-
}
9+
unsafe extern "C" fn alloc_helper(_ud: *mut libc::c_void, ptr: *mut libc::c_void,
10+
_osize: libc::size_t, nsize: libc::size_t) -> *mut libc::c_void {
11+
if nsize == 0 {
12+
libc::free(ptr as *mut libc::c_void);
13+
ptr::null_mut()
14+
} else {
15+
libc::realloc(ptr, nsize)
1816
}
1917
}
2018

2119
// panic function should panic!() so Lua doesn't abort
22-
extern "C" fn panic_helper(_L: *mut raw::lua_State) -> libc::c_int {
20+
unsafe extern "C" fn panic_helper(_L: *mut raw::lua_State) -> libc::c_int {
2321
panic!("lua error");
2422
}
2523

src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn test_describe() {
5656
s.pushcfunction(dummy);
5757
assert_eq!(s.typename(-1), "function");
5858

59-
extern "C" fn dummy(_L: *mut ::raw::lua_State) -> ::libc::c_int {
59+
unsafe extern "C" fn dummy(_L: *mut ::raw::lua_State) -> ::libc::c_int {
6060
0
6161
}
6262
}
@@ -117,7 +117,7 @@ fn test_tocfunction() {
117117
s.pushcfunction(cfunc);
118118
assert_eq!(s.tocfunction(2).map(|f| f as *const ()), Some(cfunc as *const ()));
119119

120-
extern "C" fn cfunc(_L: *mut raw::lua_State) -> libc::c_int { 0 }
120+
unsafe extern "C" fn cfunc(_L: *mut raw::lua_State) -> libc::c_int { 0 }
121121
}
122122

123123
#[test]

0 commit comments

Comments
 (0)