Skip to content

Commit e43e5e2

Browse files
FIX: add bounds checks to lua
1 parent 11185fd commit e43e5e2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/api/lua.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <lualib.h>
2929
#include <ctype.h>
3030

31+
#include "fftdata.h"
32+
3133
extern bool parse_note(const char* noteStr, s32* note, s32* octave);
3234

3335
static inline s32 getLuaNumber(lua_State* lua, s32 index)
@@ -1542,6 +1544,23 @@ static s32 lua_fft(lua_State* lua)
15421544
end_freq = getLuaNumber(lua, 2);
15431545
}
15441546

1547+
if (end_freq == -1)
1548+
{
1549+
if (start_freq < 0 || start_freq >= FFT_SIZE)
1550+
{
1551+
luaL_error(lua, "invalid params, start_freq out of bounds (max 1024)\n");
1552+
return 0;
1553+
}
1554+
}
1555+
else
1556+
{
1557+
if (start_freq < 0 || end_freq >= FFT_SIZE || start_freq > end_freq)
1558+
{
1559+
luaL_error(lua, "invalid params, range out of bounds from (min 0, max 1024)\n");
1560+
return 0;
1561+
}
1562+
}
1563+
15451564
lua_pushnumber(lua, core->api.fft(tic, start_freq, end_freq));
15461565
return 1;
15471566
}
@@ -1568,6 +1587,23 @@ static s32 lua_ffts(lua_State* lua)
15681587
end_freq = getLuaNumber(lua, 2);
15691588
}
15701589

1590+
if (end_freq == -1)
1591+
{
1592+
if (start_freq < 0 || start_freq >= FFT_SIZE)
1593+
{
1594+
luaL_error(lua, "invalid params, start_freq out of bounds (max 1024)\n");
1595+
return 0;
1596+
}
1597+
}
1598+
else
1599+
{
1600+
if (start_freq < 0 || end_freq >= FFT_SIZE || start_freq > end_freq)
1601+
{
1602+
luaL_error(lua, "invalid params, range out of bounds from (min 0, max 1024)\n");
1603+
return 0;
1604+
}
1605+
}
1606+
15711607
lua_pushnumber(lua, core->api.ffts(tic, start_freq, end_freq));
15721608
return 1;
15731609
}

0 commit comments

Comments
 (0)