Skip to main content

luaur_vm/functions/
buffer_fill.rs

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