[all-commits] [llvm/llvm-project] a3d357: [FileCheck] Use StringRef for MatchRegexp to fix c...

Florian Hahn via All-commits all-commits at lists.llvm.org
Wed Sep 1 05:28:01 PDT 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: a3d357e504875f4f6c2b3c2674eb84a7b4101cb9
      https://github.com/llvm/llvm-project/commit/a3d357e504875f4f6c2b3c2674eb84a7b4101cb9
  Author: Florian Hahn <flo at fhahn.com>
  Date:   2021-09-01 (Wed, 01 Sep 2021)

  Changed paths:
    M llvm/lib/FileCheck/FileCheck.cpp
    A llvm/test/FileCheck/invalid-regex.txt

  Log Message:
  -----------
  [FileCheck] Use StringRef for MatchRegexp to fix crash.

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.

Reviewed By: thopre

Differential Revision: https://reviews.llvm.org/D109050




More information about the All-commits mailing list