Something went wrong. Try again.
Monorepo for PackTools - tooling for Minecraft Modpacks!
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647// Luau standard-library functions packluau checks before Luau sees the arguments. Luau reads an// integer argument with an unchecked double-to-int cast (luai_num2int): undefined behaviour out// of range, and in practice a CPU-dependent answer (x86-64 INT_MIN, arm64 saturation). Luau is// never patched (Packwand .standards section 8), so a guarded function refuses such a count with// the same error everywhere; build.zig's -fno-sanitize=float-cast-overflow stays as the backstop// for every argument not guarded here. Reported upstream by the owner. AI-authored (Claude).
#include "stdlib_guards.hpp"
#include "lua.h"#include "lualib.h"
#include <cmath>
namespace packluau {
namespace {
constexpr double int_limit = 2147483648.0; // 2^31
/// `string.rep(s, n)`, with `n` checked: out of int range (or NaN) is an error, never a cast./// In range, `n` is truncated toward zero as Luau's own cast would, and the original runs.int checked_rep(lua_State *state) { luaL_checkstring(state, 1); const double count = luaL_checknumber(state, 2); if (std::isnan(count) || count >= int_limit || count < -int_limit) { luaL_error(state, "string.rep: the count must be a number from -2^31 to 2^31 - 1"); } lua_pushvalue(state, lua_upvalueindex(1)); lua_pushvalue(state, 1); lua_pushinteger(state, static_cast<int>(count)); lua_call(state, 2, 1); return 1;}
} // namespace
void install_stdlib_guards(lua_State *state) { lua_getglobal(state, "string"); lua_getfield(state, -1, "rep"); lua_pushcclosure(state, &checked_rep, "rep", 1); lua_setfield(state, -2, "rep"); lua_pop(state, 1);}
} // namespace packluau