From 1767bc4fd82bfacee622e698f9f0ae42c02126fa Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Fri, 20 Jan 2017 14:55:59 +0200 Subject: [PATCH] Preserve macro scoping level on re-entry from Lua When a %{lua:...} macro calls rpm.expand(), it re-enters the macro environment at depth zero regardless of what the "calling" nesting level was. This could lead to wrong macros getting mopped out on complex, nested lua + native macro calls. Store the depth in global macro context and pick up the initial value from there to keep the nesting level increasing on when entering %{lua:...} space. --- rpmio/macro.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rpmio/macro.c b/rpmio/macro.c index 8dd99737c..ef322de7c 100644 --- a/rpmio/macro.c +++ b/rpmio/macro.c @@ -58,6 +58,7 @@ struct rpmMacroContext_s { rpmMacroEntry *tab; /*!< Macro entry table (array of pointers). */ int n; /*!< No. of macros. */ unsigned int defcnt; /*!< Non-global define tracking */ + int depth; /*!< Depth tracking when recursing from Lua */ pthread_mutex_t lock; pthread_mutexattr_t lockattr; }; @@ -808,13 +809,17 @@ static void doLua(MacroBuf mb, const char * f, size_t fn, const char * g, size_t rpmlua lua = NULL; /* Global state. */ char *scriptbuf = xmalloc(gn + 1); char *printbuf; + rpmMacroContext mc = mb->mc; + int odepth = mc->depth; if (g != NULL && gn > 0) memcpy(scriptbuf, g, gn); scriptbuf[gn] = '\0'; rpmluaPushPrintBuffer(lua); + mc->depth = mb->depth; if (rpmluaRunScript(lua, scriptbuf, NULL) == -1) mb->error = 1; + mc->depth = odepth; printbuf = rpmluaPopPrintBuffer(lua); if (printbuf) { mbAppendStr(mb, printbuf); @@ -1305,7 +1310,7 @@ static int doExpandMacros(rpmMacroContext mc, const char *src, char **target) int rc = 0; mb->buf = NULL; - mb->depth = 0; + mb->depth = mc->depth; mb->macro_trace = print_macro_trace; mb->expand_trace = print_expand_trace; mb->mc = mc;