diff mbox series

Lua: evaluate return code for image read callback

Message ID 20240801065249.206967-1-stefano.babic@swupdate.org
State Accepted
Headers show
Series Lua: evaluate return code for image read callback | expand

Commit Message

Stefano Babic Aug. 1, 2024, 6:52 a.m. UTC
On custom design handler, a Lua callback can be set when an image should
be read. The lua interface just checks that the Lua callback can be
executed, but it does not check if the callback returns with an error
code, and SWUpdate cannot detect this. Check return code in istream_read_callback
instead of just returning success.

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 corelib/lua_interface.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

--
2.34.1
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index f2ef849f..35433b9c 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -424,6 +424,7 @@  copyfile_exit:
 static int istream_read_callback(void *out, const void *buf, size_t len)
 {
 	lua_State* L = (lua_State*)out;
+	lua_Number result;
 	if (len > LUAL_BUFFERSIZE) {
 		ERROR("I/O buffer size is larger than Lua's buffer size %d", LUAL_BUFFERSIZE);
 		return -1;
@@ -438,12 +439,23 @@  static int istream_read_callback(void *out, const void *buf, size_t len)
 	memcpy(buffer, buf, len);
 	luaL_addsize(&lbuffer, len);
 	luaL_pushresult(&lbuffer);
-	if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
+	if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
 		ERROR("Lua error in callback: %s", lua_tostring(L, -1));
 		lua_pop(L, 1);
 		return -1;
 	}
-	return 0;
+
+	/* retrieve result */
+	if (!lua_isnumber(L, -1)) {
+		ERROR("Lua Callback must return a number");
+		lua_pop(L, 1);
+		return -1;
+	}
+
+	result = lua_tonumber(L, -1);
+	lua_pop(L, 1);
+
+	return (int) result;
 }

 static int l_istream_read(lua_State* L)