Skip to content

Commit

Permalink
Cxx: use a trashbox for deleteing tokens allocated for anonymous para…
Browse files Browse the repository at this point in the history
…meters

Partially close #3372.

The original code assumed tokens allocated in
cxxParserTokenChainLooksLikeFunctionParameterList() for tagging
anonymous parameters were deleted on its caller side
(cxxParserEmitFunctionParameterTags()).

However, in some conditions, cxxParserEmitFunctionParameterTags() are
not called. As a result, the allocated tokens are not deleted.

To avoid the memory leaking, the deletion cannot depend on
cxxParserEmitFunctionParameterTags(). This change uses per-parsing
trashbox for deleting the anonymous tokens. The objects linked to
the trashbox are destroyed at the end of parsing the current input
file.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed May 6, 2022
1 parent 6a311c2 commit 55ba726
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test case for detecting memory leaking.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--kinds-C=+z
--extras=+{anonymous}
16 changes: 16 additions & 0 deletions Units/parser-c.r/anonymous-param-in-broken-paramlist.d/input-0.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* https://gitlab.gnome.org/GNOME/glib/-/blob/main/gio/gdummyproxyresolver.c */
G_DEFINE_TYPE_WITH_CODE (GDummyProxyResolver, g_dummy_proxy_resolver, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER,
g_dummy_proxy_resolver_iface_init)
_g_io_modules_ensure_extension_points_registered ();
g_io_extension_point_implement (G_PROXY_RESOLVER_EXTENSION_POINT_NAME,
g_define_type_id,
"dummy",
-100))

static void
g_dummy_proxy_resolver_finalize (GObject *object)
{
/* must chain up */
G_OBJECT_CLASS (g_dummy_proxy_resolver_parent_class)->finalize (object);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
G (f () g ()) h (void) {
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f(a,){
5 changes: 4 additions & 1 deletion parsers/cxx/cxx_parser_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,8 @@ void cxxParserEmitFunctionParameterTags(CXXTypedVariableSet * pInfo)

if(pTypeName)
{
if (pInfo->uAnonymous & (0x1u << i))
PARSER_TRASH_BOX_TAKE_BACK (pInfo->aIdentifiers[i]);
cxxTokenDestroy(pInfo->aIdentifiers[i]);
cxxTokenDestroy(pTypeName);
}
Expand Down Expand Up @@ -2180,6 +2182,7 @@ bool cxxParserTokenChainLooksLikeFunctionParameterList(
pIdentifier->iLineNumber = t->pPrev->iLineNumber;
pIdentifier->oFilePosition = t->pPrev->oFilePosition;
pParamInfo->uAnonymous |= (0x1u << pParamInfo->uCount);
PARSER_TRASH_BOX (pIdentifier, cxxTokenDestroy);
}
pParamInfo->aIdentifiers[pParamInfo->uCount] = pIdentifier;
pParamInfo->uCount++;
Expand Down Expand Up @@ -2233,7 +2236,7 @@ bool cxxParserTokenChainLooksLikeFunctionParameterList(
pParamInfo->uCount++;

PARSER_TRASH_BOX (pFakeStart, cxxTokenDestroy);
/* pFakeId may be destroyed via pParamInfo->aIdentifiers[i]. */
PARSER_TRASH_BOX (pFakeId, cxxTokenDestroy);
}

if(cxxTokenTypeIs(t,CXXTokenTypeClosingParenthesis))
Expand Down

0 comments on commit 55ba726

Please sign in to comment.