Skip to main content

luaur_vm/functions/
buffer_copy.rs

1use crate::functions::lua_l_checkbuffer::lua_l_checkbuffer;
2use crate::functions::lua_l_checkinteger::lua_l_checkinteger;
3use crate::functions::lua_l_optinteger::lua_l_optinteger;
4use crate::macros::isoutofbounds::isoutofbounds;
5use crate::macros::lua_l_error::luaL_error;
6use crate::type_aliases::lua_state::lua_State;
7
8pub fn buffer_copy(L: *mut lua_State) -> core::ffi::c_int {
9    let mut tlen: usize = 0;
10    let tbuf = lua_l_checkbuffer(L, 1, &mut tlen);
11    let toffset = lua_l_checkinteger(L, 2);
12
13    let mut slen: usize = 0;
14    let sbuf = lua_l_checkbuffer(L, 3, &mut slen);
15    let soffset = lua_l_optinteger(L, 4, 0);
16
17    // C++ evaluates `int(slen) - soffset` as the default eagerly (signed overflow
18    // is UB upstream for soffset = INT_MIN); wrapping_sub reproduces the two's-
19    // complement value C++ relies on, which the `size < 0` / isoutofbounds checks
20    // below then reject. (Upstream UBSan: lbuflib.cpp:257.)
21    let size = lua_l_optinteger(L, 5, (slen as core::ffi::c_int).wrapping_sub(soffset));
22
23    if size < 0 {
24        luaL_error!(L, "buffer access out of bounds");
25    }
26
27    if isoutofbounds(soffset, slen, size as usize) {
28        luaL_error!(L, "buffer access out of bounds");
29    }
30
31    if isoutofbounds(toffset, tlen, size as usize) {
32        luaL_error!(L, "buffer access out of bounds");
33    }
34
35    unsafe {
36        core::ptr::copy(
37            (sbuf as *const core::ffi::c_char).offset(soffset as isize),
38            (tbuf as *mut core::ffi::c_char).offset(toffset as isize),
39            size as usize,
40        );
41    }
42
43    0
44}