Skip to content

Commit

Permalink
Add copy control and in particular, destructor to the macro context
Browse files Browse the repository at this point in the history
The macro contexts being static structs, they get their destructor
automatically called at program end. Which counter-intuitively *causes*
a leak if macros are stored in an STL container and rpmFreeMacros()
isn't called, because then the container clears itself and the macros
are left dangling, whereas without the destructor they are still
reachable. freeMacros() obviously wants to be a member function but
trying to keep things in the struct land for now.

Doesn't really change anything as of now, but this is needed for moving
the macro storage to C++ container.
  • Loading branch information
pmatilai committed Apr 30, 2024
1 parent 667e32c commit e571df4
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions rpmio/macro.c
Expand Up @@ -69,8 +69,14 @@ struct rpmMacroContext_s {
int level; /*!< Scope level tracking when on external recursion */
pthread_mutex_t lock;
pthread_mutexattr_t lockattr;
};

rpmMacroContext_s() = default;
rpmMacroContext_s(const rpmMacroContext_s &other) = delete;
rpmMacroContext_s(const rpmMacroContext_s &&other) = delete;
rpmMacroContext_s &operator =(const rpmMacroContext_s & other) = delete;
rpmMacroContext_s &operator =(const rpmMacroContext_s && other) = delete;
~rpmMacroContext_s();
};

static struct rpmMacroContext_s rpmGlobalMacroContext_s;
rpmMacroContext rpmGlobalMacroContext = &rpmGlobalMacroContext_s;
Expand Down Expand Up @@ -144,6 +150,20 @@ static rpmMacroEntry popMacro(rpmMacroContext mc, const char * n);
static int loadMacroFile(rpmMacroContext mc, const char * fn);
/* =============================================================== */

static void freeMacros(rpmMacroContext mc)
{
while (mc->n > 0) {
/* remove from the end to avoid memmove */
rpmMacroEntry me = mc->tab[mc->n - 1];
while ((me = popMacro(mc, me->name))) {}
}
}

rpmMacroContext_s::~rpmMacroContext_s()
{
freeMacros(this);
}

static rpmMacroContext rpmmctxAcquire(rpmMacroContext mc)
{
if (mc == NULL)
Expand Down Expand Up @@ -2189,11 +2209,7 @@ void
rpmFreeMacros(rpmMacroContext mc)
{
mc = rpmmctxAcquire(mc);
while (mc->n > 0) {
/* remove from the end to avoid memmove */
rpmMacroEntry me = mc->tab[mc->n - 1];
popMacro(mc, me->name);
}
freeMacros(mc);
rpmmctxRelease(mc);
}

Expand Down

0 comments on commit e571df4

Please sign in to comment.