Skip to content

Commit

Permalink
CXX: Avoid accessing token after it has been destroyed (#2586)
Browse files Browse the repository at this point in the history
Fixes #2554
Replaces #2559
  • Loading branch information
pragmaware committed Jul 6, 2020
1 parent 1cae951 commit b4574f0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions Units/parser-c.r/bug2554.c.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--sort=no
7 changes: 7 additions & 0 deletions Units/parser-c.r/bug2554.c.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__anonbbb2b4710108 input.c /^{$/;" s file:
n input.c /^ int*n;$/;" m struct:__anonbbb2b4710108 typeref:typename:int * file:
S input.c /^} S;$/;" t typeref:struct:__anonbbb2b4710108 file:
foo input.c /^struct foo {$/;" s file:
bar input.c /^ int bar;$/;" m struct:foo typeref:typename:int file:
v input.c /^} v = {$/;" v typeref:struct:foo
w input.c /^struct foo w = {$/;" v typeref:struct:foo
22 changes: 22 additions & 0 deletions Units/parser-c.r/bug2554.c.d/input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* valgrind reported an error when ctags built with
* --enable-debugging configure option.
*
* To reproduce:
*
* $ make units LANGUAGES=C VG=1
*
*/
typedef struct
{
int*n;
} S;

struct foo {
int bar;
} v = {
.bar = 1,
};

struct foo w = {
.bar = 2,
};
38 changes: 17 additions & 21 deletions parsers/cxx/cxx_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,11 @@ static bool cxxParserParseEnumStructClassOrUnionFullDeclarationTrailer(
MIOPos oFilePosition = getInputFilePosition();
int iFileLine = getInputLineNumber();

if(!cxxParserParseUpToOneOf(CXXTokenTypeEOF | CXXTokenTypeSemicolon | CXXTokenTypeOpeningBracket
| CXXTokenTypeAssignment, false))
if(!cxxParserParseUpToOneOf(
CXXTokenTypeEOF | CXXTokenTypeSemicolon |
CXXTokenTypeOpeningBracket | CXXTokenTypeAssignment,
false
))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse up to EOF/semicolon");
return false;
Expand Down Expand Up @@ -556,30 +559,23 @@ static bool cxxParserParseEnumStructClassOrUnionFullDeclarationTrailer(
return true;
}

if(cxxTokenTypeIs(g_cxx.pToken,CXXTokenTypeAssignment))
{
if(!cxxParserParseUpToOneOf(
CXXTokenTypeEOF | CXXTokenTypeSemicolon,
false
))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse up to EOF/semicolon");
return false;
}
}

if(uKeywordState & CXXParserKeywordStateSeenTypedef)
cxxParserExtractTypedef(g_cxx.pTokenChain,true);
else
cxxParserExtractVariableDeclarations(g_cxx.pTokenChain,0);

/*
Skip initializer in
struct foo { ... } x = { ... };
if we are at --------^.
we go to ---------------------^.
The above example is known case.
To be tolerant and handle unrecognized case, we
put the code for skipping here. */
if(cxxTokenTypeIs(g_cxx.pToken,CXXTokenTypeAssignment) &&
(!cxxParserParseUpToOneOf(CXXTokenTypeEOF | CXXTokenTypeSemicolon, true)))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse up to EOF/semicolon");
return false;
}


CXX_DEBUG_LEAVE();
return true;
}
Expand Down

0 comments on commit b4574f0

Please sign in to comment.