Skip to content

Commit 9249f89

Browse files
philmozpfeerick
authored andcommitted
fix(color): add error handling for Lua LVGL object creation (#7215)
1 parent b56c14e commit 9249f89

4 files changed

Lines changed: 37 additions & 31 deletions

File tree

radio/src/gui/colorlcd/mainview/widget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ inline WidgetOptionValueEnum widgetValueEnumFromType(WidgetOption::Type type)
5050

5151
case WidgetOption::Color:
5252
return WOV_Color;
53-
53+
5454
default:
5555
return WOV_Unsigned;
5656
}
@@ -207,7 +207,7 @@ void Widget::onCancel()
207207

208208
void Widget::setFullscreen(bool enable)
209209
{
210-
if (!fsAllowed || (enable == fullscreen)) return;
210+
if (parent->isTopBar() || (enable == fullscreen)) return;
211211

212212
fullscreen = enable;
213213

radio/src/lua/api_colorlcd_lvgl.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ class LvglWidgetParams
3838
if (!strcmp(key, "type")) {
3939
if (lua_isinteger(L, -1)) {
4040
int n = lua_tointeger(L, -1);
41-
if (n > ETX_UNDEF && n < ETX_LAST)
41+
if (n > ETX_UNDEF && n < ETX_LAST) {
4242
type = (LuaLvglType)n;
43-
else
43+
} else {
44+
luaL_error(L, "Invalid type '%d'", n);
4445
type = ETX_UNDEF;
46+
}
4547
} else {
46-
type = getType(luaL_checkstring(L, -1));
48+
const char* s = luaL_checkstring(L, -1);
49+
type = getType(s);
50+
if (type == ETX_UNDEF)
51+
luaL_error(L, "Invalid type '%s'", s);
4752
}
4853
} else if (!strcmp(key, "name")) {
4954
name = luaL_checkstring(L, -1);
@@ -184,6 +189,8 @@ static void buildLvgl(lua_State *L, int srcIndex, int refIndex)
184189
for (lua_pushnil(L); lua_next(L, srcIndex - 1); lua_pop(L, 1)) {
185190
auto t = lua_gettop(L);
186191
LvglWidgetParams p(L, -1);
192+
if (p.type == ETX_UNDEF)
193+
luaL_error(L, "Missing or bad type");
187194
if (p.type >= ETX_FIRST_CONTROL && !luaScriptManager->isFullscreen())
188195
continue;
189196
LvglWidgetObjectBase *obj = nullptr;

radio/src/lua/interface.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ extern "C" {
5555
// #include "touch.h"
5656
// #endif
5757

58-
// Since we may not run FG every time, keep the events in a buffer
59-
event_t events[EVENT_BUFFER_SIZE] = { 0 };
6058
// The main thread - lsScripts is now a coroutine
6159
lua_State * mainState = nullptr;
6260
lua_State *lsScripts = nullptr;
@@ -327,14 +325,10 @@ void luaDoGc(lua_State * L, bool full)
327325
void luaFree(lua_State * L, ScriptInternalData & sid)
328326
{
329327
PROTECT_LUA() {
330-
if (sid.run) {
331-
luaL_unref(L, LUA_REGISTRYINDEX, sid.run);
332-
sid.run = 0;
333-
}
334-
if (sid.background) {
335-
luaL_unref(L, LUA_REGISTRYINDEX, sid.background);
336-
sid.background = 0;
337-
}
328+
luaL_unref(L, LUA_REGISTRYINDEX, sid.run);
329+
sid.run = LUA_REFNIL;
330+
luaL_unref(L, LUA_REGISTRYINDEX, sid.background);
331+
sid.background = LUA_REFNIL;
338332
}
339333
else {
340334
luaDisable();
@@ -857,7 +851,7 @@ static int luaRegisterFunction(const char * key)
857851
TRACE_ERROR("luaRegisterFunction(%s): Error: '%.*s' is not a function\n", LEN_SCRIPT_FILENAME, getScriptName(luaScriptsCount - 1), key);
858852
}
859853
lua_pop(lsScripts, 1);
860-
return LUA_NOREF;
854+
return LUA_REFNIL;
861855
}
862856
}
863857

@@ -877,7 +871,7 @@ static void luaLoadScripts(bool init, const char * filename = nullptr)
877871
if (luaState == INTERPRETER_PANIC) return;
878872

879873
luaLcdAllowed = false;
880-
initFunction = LUA_NOREF;
874+
initFunction = LUA_REFNIL;
881875
luaEmptyEventBuffer();
882876

883877
// Initialize loop over references
@@ -949,11 +943,11 @@ static void luaLoadScripts(bool init, const char * filename = nullptr)
949943
}
950944
else if (luaStatus == LUA_OK) {
951945
// Coroutine returned
952-
if (initFunction != LUA_NOREF) {
946+
if (initFunction != LUA_REFNIL) {
953947
// init() returned - clean up
954948
luaL_unref(lsScripts, LUA_REGISTRYINDEX, initFunction);
955949
lua_settop(lsScripts, 0);
956-
initFunction = LUA_NOREF;
950+
initFunction = LUA_REFNIL;
957951
}
958952
else {
959953
// chunk() returned
@@ -964,10 +958,10 @@ static void luaLoadScripts(bool init, const char * filename = nullptr)
964958
sid.run = luaRegisterFunction("run");
965959
sid.background = luaRegisterFunction("background");
966960
initFunction = luaRegisterFunction("init");
967-
if (sid.run == LUA_NOREF) {
961+
if (sid.run == LUA_REFNIL) {
968962
snprintf(lua_warning_info, LUA_WARNING_INFO_LEN, "luaLoadScripts(%.*s): No run function\n", LEN_SCRIPT_FILENAME, getScriptName(idx));
969963
sid.state = SCRIPT_SYNTAX_ERROR;
970-
initFunction = LUA_NOREF;
964+
initFunction = LUA_REFNIL;
971965
}
972966
#if defined(LUA_MODEL_SCRIPTS)
973967
// Get input/output tables for mixer scripts
@@ -985,14 +979,14 @@ static void luaLoadScripts(bool init, const char * filename = nullptr)
985979
else {
986980
snprintf(lua_warning_info, LUA_WARNING_INFO_LEN, "luaLoadScripts(%.*s): The script did not return a table\n", LEN_SCRIPT_FILENAME, getScriptName(idx));
987981
sid.state = SCRIPT_SYNTAX_ERROR;
988-
initFunction = LUA_NOREF;
982+
initFunction = LUA_REFNIL;
989983
}
990984

991985
// Pop the table off the stack
992986
lua_pop(lsScripts, 1);
993987

994988
// If init(), push it on the stack
995-
if (initFunction != LUA_NOREF) {
989+
if (initFunction != LUA_REFNIL) {
996990
lua_rawgeti(lsScripts, LUA_REGISTRYINDEX, initFunction);
997991
#if !defined(COLORLCD)
998992
if (ref == SCRIPT_STANDALONE) {
@@ -1005,9 +999,9 @@ static void luaLoadScripts(bool init, const char * filename = nullptr)
1005999
else {
10061000
// Error
10071001
sid.state = SCRIPT_SYNTAX_ERROR;
1008-
initFunction = LUA_NOREF;
1002+
initFunction = LUA_REFNIL;
10091003
}
1010-
} while(initFunction != LUA_NOREF);
1004+
} while(initFunction != LUA_REFNIL);
10111005

10121006
if (sid.state != SCRIPT_OK) {
10131007
luaError(lsScripts, sid.state);
@@ -1149,14 +1143,14 @@ static bool resumeLua(bool init, bool allowLcdUsage)
11491143
functionsContext->lastFunctionTime[idx] = tmr10ms;
11501144
}
11511145
else {
1152-
if (sid.background == LUA_NOREF) continue;
1146+
if (sid.background == LUA_REFNIL) continue;
11531147
lua_rawgeti(lsScripts, LUA_REGISTRYINDEX, sid.background);
11541148
}
11551149
} else continue;
11561150
}
11571151
#if defined(PCBTARANIS)
11581152
else if (ref <= SCRIPT_TELEMETRY_LAST) {
1159-
if (sid.background == LUA_NOREF) continue;
1153+
if (sid.background == LUA_REFNIL) continue;
11601154
lua_rawgeti(lsScripts, LUA_REGISTRYINDEX, sid.background);
11611155
}
11621156
#endif

radio/src/lua/lua_lvgl_widget.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ LvglWidgetObjectBase *LvglWidgetObjectBase::checkLvgl(lua_State *L, int index, b
334334
if (p && *p) return *p;
335335

336336
if (required) {
337-
luaL_error(L, "Invalid lvgl object (it has been probably been cleared).");
337+
luaL_error(L, "Invalid object (it has been probably been cleared).");
338338
}
339339

340340
return nullptr;
@@ -651,6 +651,8 @@ void LvglWidgetObjectBase::parseParam(lua_State *L, const char *key)
651651
getPosFunction = ::getRef(L, LUA_REGISTRYINDEX);
652652
} else if (!strcmp(key, "floating")) {
653653
floating = getLuaBool(L);
654+
} else if (strcmp(key, "children") && strcmp(key, "type") && strcmp(key, "name")) {
655+
luaL_error(L, "Invalid property '%s'", key);
654656
}
655657
}
656658

@@ -855,9 +857,12 @@ void LvglWidgetLabel::setAlign(LcdFlags newAlign)
855857
{
856858
if (lvobj) {
857859
align.flags = newAlign;
858-
if (align.flags & VCENTERED) {
859-
lv_obj_align(lvobj, LV_ALIGN_LEFT_MID, 0, 0);
860-
}
860+
if (align.flags & VCENTERED)
861+
lv_obj_set_style_align(lvobj, LV_ALIGN_LEFT_MID, LV_PART_MAIN);
862+
else if (align.flags & VTOP)
863+
lv_obj_set_style_align(lvobj, LV_ALIGN_TOP_LEFT, LV_PART_MAIN);
864+
else if (align.flags & VBOTTOM)
865+
lv_obj_set_style_align(lvobj, LV_ALIGN_BOTTOM_LEFT, LV_PART_MAIN);
861866
lv_obj_set_style_text_align(lvobj,
862867
(align.flags & RIGHT) ? LV_TEXT_ALIGN_RIGHT
863868
: (align.flags & CENTERED) ? LV_TEXT_ALIGN_CENTER

0 commit comments

Comments
 (0)