This is an archive of the discontinued LLVM Phabricator instance.

PR23057: fix use-after-free due to local token buffer in ParseCXXAmbiguousParenExpression
ClosedPublic

Authored by DmitryPolukhin on Jan 26 2016, 2:45 AM.

Details

Summary

To completely eliminate use-after-free in this place I had to copy tokens into new array and pass ownership. As far as I understand the code it is not possible to guarantee that all inserted tokens are consumed before exiting from this function. Depending on how many tokens were consumed before SkipUntil is called, it may not happen due to unbalanced brackets, etc. Other places where EnterTokenStream is called usually pass vector that some class owns but here it is not possible because this place can be called recursively.

Diff Detail

Event Timeline

DmitryPolukhin retitled this revision from to PR23057: fix use-after-free due to local token buffer in ParseCXXAmbiguousParenExpression.
DmitryPolukhin updated this object.
DmitryPolukhin added a reviewer: rjmccall.
DmitryPolukhin added a subscriber: cfe-commits.

I don't fully understand your explanation. Is your change introducing a
memory leak? (or was the old code causing a double free (if
EnterTokenStream does take ownership of this argument)in addition to
use-after-free?)

I didn't introduce a leak because I pass ownership of the Buffer to EnterTokenStream (i.e. pass true as the last argument OwnsTokens). ASan also doesn't report a leak with my patch. Original code didn't have double free because it used call EnterTokenStream with OwnsTokens == false.

DmitryPolukhin added a subscriber: kcc.

I believe this change is correct but I believe we can handle this a little more efficiently.

I wonder if it would be better if we instead inserted a special EOF token at the end of Toks. If any errors occur, consume tokens until we hit our special EOF token. ParseLexedAttribute and ParseLexedMethodDeclaration follow this pattern. To make it a little more obvious that the lifetime of Toks is somehow tied to EnterTokenStream and the parsing code that follows, it might make sense to factor the code after EnterTokenStream into another function.

Use EOF token instead of copy buffer. This approach looks a bit more fragile but definitely more efficient, PTAL.

majnemer accepted this revision.Feb 2 2016, 8:32 AM
majnemer added a reviewer: majnemer.

LGTM

This revision is now accepted and ready to land.Feb 2 2016, 8:32 AM