Blob Blame History Raw
From 13c96b26978953aff17bd642328d249820214b12 Mon Sep 17 00:00:00 2001
Message-Id: <13c96b26978953aff17bd642328d249820214b12.1620391180.git.pmatilai@redhat.com>
From: Panu Matilainen <pmatilai@redhat.com>
Date: Fri, 7 May 2021 15:10:13 +0300
Subject: [PATCH] Fix regression wrt Lua reinitialization (RhBug:1958095)

Commit 2579d3e5ad5d713f2c161b9fb4835366ea4ea291 started storing the Lua
context in the spec, but this leads to problems as what is actually a
global context is now stored in two places, and can get out of sync.
So if you parse a spec, and then reset the global context, you get a
fancy segfault when the freeing the spec because it's pointing to
la-la-lua land.

Revert back to always using the global Lua handle.
---
 build/parsePreamble.c     | 3 ++-
 build/rpmbuild_internal.h | 1 -
 build/spec.c              | 4 ++--
 build/speclua.c           | 2 +-
 tests/rpmpython.at        | 7 ++++++-
 5 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/build/parsePreamble.c b/build/parsePreamble.c
index fc4a2994d..ac3d9159e 100644
--- a/build/parsePreamble.c
+++ b/build/parsePreamble.c
@@ -321,7 +321,8 @@ int addSource(rpmSpec spec, int specline, const char *srcname, rpmTagVal tag)
     rpmPushMacro(spec->macros, buf, NULL, p->fullSource, RMIL_SPEC);
     free(buf);
 
-    addLuaSource(spec->lua, p);
+    rpmlua lua = rpmluaGetGlobalState();
+    addLuaSource(lua, p);
 
     if (!nofetch && tryDownload(p))
 	return RPMRC_FAIL;
diff --git a/build/rpmbuild_internal.h b/build/rpmbuild_internal.h
index 2bac303c7..39a9c916f 100644
--- a/build/rpmbuild_internal.h
+++ b/build/rpmbuild_internal.h
@@ -138,7 +138,6 @@ struct rpmSpec_s {
     Package sourcePackage;
 
     rpmMacroContext macros;
-    rpmlua lua;
     rpmstrPool pool;
 
     StringBuf prep;		/*!< %prep scriptlet. */
diff --git a/build/spec.c b/build/spec.c
index 365ce6d32..6a13afda2 100644
--- a/build/spec.c
+++ b/build/spec.c
@@ -249,7 +249,7 @@ rpmSpec newSpec(void)
     spec->macros = rpmGlobalMacroContext;
     spec->pool = rpmstrPoolCreate();
     
-    spec->lua = specLuaInit(spec);
+    specLuaInit(spec);
     return spec;
 }
 
@@ -298,7 +298,7 @@ rpmSpec rpmSpecFree(rpmSpec spec)
 
     // only destroy lua tables if there are no BASpecs left
     if (spec->recursing || spec->BACount == 0) {
-	spec->lua = specLuaFini(spec);
+	specLuaFini(spec);
     }
 
     spec->sources = freeSources(spec->sources);
diff --git a/build/speclua.c b/build/speclua.c
index 595d71f14..abf2a4886 100644
--- a/build/speclua.c
+++ b/build/speclua.c
@@ -25,7 +25,7 @@ void * specLuaInit(rpmSpec spec)
 
 void * specLuaFini(rpmSpec spec)
 {
-    rpmlua lua = spec->lua;
+    rpmlua lua = rpmluaGetGlobalState();
     lua_State *L = rpmluaGetLua(lua);
     for (const char **vp = luavars; vp && *vp; vp++) {
 	lua_pushnil(L);
diff --git a/tests/rpmpython.at b/tests/rpmpython.at
index 14c6a75dc..8128263e3 100644
--- a/tests/rpmpython.at
+++ b/tests/rpmpython.at
@@ -67,7 +67,7 @@ for iot in [ 'fpio', 'fdio', 'ufdio', 'gzdio' ]:
 ],
 [])
 
-RPMPY_TEST([spec parse],[
+RPMPY_TEST([spec parse 1],[
 # TODO: add a better test spec with sub-packages etc
 spec = rpm.spec('${RPMDATA}/SPECS/hello.spec')
 for (name, num, flags) in spec.sources:
@@ -82,6 +82,11 @@ hello-1.0-1
 hello-1.0-1
 ])
 
+RPMPY_TEST([spec parse 2],[
+spec = rpm.spec('${RPMDATA}/SPECS/mini.spec')
+rpm.reloadConfig()
+])
+
 RPMPY_TEST([basic header manipulation],[
 h = rpm.hdr()
 h['name'] = 'testpkg'
-- 
2.30.2