From d41143cb5f6d88eb6e8bd999ad5ea2992bfb10f7 Mon Sep 17 00:00:00 2001
From: Panu Matilainen <pmatilai@redhat.com>
Date: Thu, 18 Nov 2021 13:38:20 +0200
Subject: [PATCH] Simplify rpm_print(), fixing a Lua stack leak as a bonus

Rather than laborously call tostring() in Lua, use the C-side equivalent
of luaL_tostring(). This was new as of Lua 5.2, which explains why the
original version from 2004 did things the hard way.

Also fixes a stack leak from not popping "tostring" function after use.
---
 rpmio/rpmlua.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/rpmio/rpmlua.c b/rpmio/rpmlua.c
index 6ad9119..7402307 100644
--- a/rpmio/rpmlua.c
+++ b/rpmio/rpmlua.c
@@ -688,16 +688,9 @@ static int rpm_print (lua_State *L)
     int n = lua_gettop(L);  /* number of arguments */
     int i;
     if (!lua) return 0;
-    lua_getglobal(L, "tostring");
     for (i = 1; i <= n; i++) {
-	const char *s;
 	size_t sl;
-	lua_pushvalue(L, -1);  /* function to be called */
-	lua_pushvalue(L, i);   /* value to print */
-	lua_call(L, 1, 1);
-	s = lua_tolstring(L, -1, &sl);  /* get result */
-	if (s == NULL)
-	    return luaL_error(L, "`tostring' must return a string to `print'");
+	const char *s = luaL_tolstring(L, i, &sl);
 	if (lua->printbuf) {
 	    rpmluapb prbuf = lua->printbuf;
 	    if (prbuf->used+sl+1 > prbuf->alloced) {
-- 
1.8.3.1

