Skip to content

Commit

Permalink
FIX: add bounds checks to lua
Browse files Browse the repository at this point in the history
  • Loading branch information
aliceisjustplaying committed May 10, 2024
1 parent 11185fd commit e43e5e2
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/api/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <lualib.h>
#include <ctype.h>

#include "fftdata.h"

extern bool parse_note(const char* noteStr, s32* note, s32* octave);

static inline s32 getLuaNumber(lua_State* lua, s32 index)
Expand Down Expand Up @@ -1542,6 +1544,23 @@ static s32 lua_fft(lua_State* lua)
end_freq = getLuaNumber(lua, 2);
}

if (end_freq == -1)
{
if (start_freq < 0 || start_freq >= FFT_SIZE)
{
luaL_error(lua, "invalid params, start_freq out of bounds (max 1024)\n");
return 0;
}
}
else
{
if (start_freq < 0 || end_freq >= FFT_SIZE || start_freq > end_freq)
{
luaL_error(lua, "invalid params, range out of bounds from (min 0, max 1024)\n");
return 0;
}
}

lua_pushnumber(lua, core->api.fft(tic, start_freq, end_freq));
return 1;
}
Expand All @@ -1568,6 +1587,23 @@ static s32 lua_ffts(lua_State* lua)
end_freq = getLuaNumber(lua, 2);
}

if (end_freq == -1)
{
if (start_freq < 0 || start_freq >= FFT_SIZE)
{
luaL_error(lua, "invalid params, start_freq out of bounds (max 1024)\n");
return 0;
}
}
else
{
if (start_freq < 0 || end_freq >= FFT_SIZE || start_freq > end_freq)
{
luaL_error(lua, "invalid params, range out of bounds from (min 0, max 1024)\n");
return 0;
}
}

lua_pushnumber(lua, core->api.ffts(tic, start_freq, end_freq));
return 1;
}
Expand Down

0 comments on commit e43e5e2

Please sign in to comment.