This is an archive of the discontinued LLVM Phabricator instance.

[FileCheck] Use StringRef for MatchRegexp to fix crash.
ClosedPublic

Authored by fhahn on Sep 1 2021, 1:55 AM.

Details

Summary

If MatchRegexp is an invalid regex, an error message will be printed
using SourceManager::PrintMessage via AddRegExToRegEx.

PrintMessage relies on the input being a StringRef into a string managed
by SourceManager. At the moment, a StringRef to a std::string
allocated in the caller of AddRegExToRegEx is passed. If the regex is
invalid, this StringRef is passed to PrintMessage, where it will crash,
because it does not point to a string managed via SourceMgr.

This patch fixes the crash by turning MatchRegexp into a StringRef If
we use MatchStr, we directly use that StringRef, which points into a
string from SourceMgr. Otherwise, MatchRegexp gets assigned
Format.getWildcardRegex(), which returns a std::string. To extend the
lifetime, assign it to a std::string variable WildcardRegexp and assign
MatchRegexp to a stringref to WildcardRegexp. WildcardRegexp should
always be valid, so we should never have to print an error message
via the SoureMgr I think.

Fixes PR49319.

Diff Detail

Event Timeline

fhahn created this revision.Sep 1 2021, 1:55 AM
fhahn requested review of this revision.Sep 1 2021, 1:55 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 1 2021, 1:55 AM
thopre accepted this revision.Sep 1 2021, 2:03 AM

LGTM, thanks!

This revision is now accepted and ready to land.Sep 1 2021, 2:03 AM
This revision was landed with ongoing or failed builds.Sep 1 2021, 5:27 AM
This revision was automatically updated to reflect the committed changes.