diff --git a/src/lib_base.c b/src/lib_base.c index ad151975f1..23728d6c9d 100644 --- a/src/lib_base.c +++ b/src/lib_base.c @@ -144,6 +144,8 @@ LJLIB_CF(getfenv) LJLIB_REC(.) cTValue *o = L->base; if (!(o < L->top && tvisfunc(o))) { int level = lj_lib_optint(L, 1, 1); + if (level < 0) + lj_err_arg(L, 1, LJ_ERR_INVLVL); o = lj_debug_frame(L, level, &level); if (o == NULL) lj_err_arg(L, 1, LJ_ERR_INVLVL); @@ -166,6 +168,8 @@ LJLIB_CF(setfenv) setgcref(L->env, obj2gco(t)); return 0; } + if (level < 0) + lj_err_arg(L, 1, LJ_ERR_INVLVL); o = lj_debug_frame(L, level, &level); if (o == NULL) lj_err_arg(L, 1, LJ_ERR_INVLVL); diff --git a/test/tarantool-tests/lj-1329-getfenv-setfenv-negative.test.lua b/test/tarantool-tests/lj-1329-getfenv-setfenv-negative.test.lua new file mode 100644 index 0000000000..bc10c16f9b --- /dev/null +++ b/test/tarantool-tests/lj-1329-getfenv-setfenv-negative.test.lua @@ -0,0 +1,27 @@ +local tap = require('tap') + +-- The test file to demonstrate UBSan warning for `setfenv()` and +-- `getfenv()` with a huge `level` value. +-- See also: https://github.com/LuaJIT/LuaJIT/issues/1329. +local test = tap.test('lj-1329-getfenv-setfenv-negative') + +test:plan(4) + +-- This number will be equal to `INT_MIN` when casted to `int`. +-- After this, it will be decremented in `lj_debug_level()` and +-- underflowed to the `INT_MAX`. That produces the UBSan warning +-- about signed integer overflow. +local LEVEL = 2 ^ 31 +local ERRMSG = 'invalid level' + +-- Tests check the UBSan runtime error. Add assertions just to be +-- sure that we don't change the behaviour. +local status, errmsg = pcall(getfenv, LEVEL) +test:ok(not status, 'getfenv: correct status') +test:like(errmsg, ERRMSG, 'getfenv: correct error message') + +status, errmsg = pcall(setfenv, LEVEL, {}) +test:ok(not status, 'setfenv: correct status') +test:like(errmsg, ERRMSG, 'setfenv: correct error message') + +test:done(true)