[llvm] [objcopy] Return an error in case of an invalid regex (PR #74319)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 5 01:00:54 PST 2023


================
@@ -39,8 +40,15 @@ NameOrPattern::create(StringRef Pattern, MatchStyle MS,
   }
   case MatchStyle::Regex: {
     SmallVector<char, 32> Data;
-    return NameOrPattern(std::make_shared<Regex>(
-        ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)));
+    auto AnchoredPattern =
+        ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data);
+    auto RegEx = std::make_shared<Regex>(AnchoredPattern);
+    std::string Err;
+    if (!RegEx->isValid(Err))
----------------
jh7370 wrote:

If I'm not mistaken, this will miss the case where the input regex ends with an unescaped backslash, because it will end up escaping the `$` character that is added. This in turn may cause some surprises with the regex matching. I wonder if it's worth adding something to check for this edge case, e.g. checking to see if the original string was a valid regex or simply checking the last couple of bytes to make sure there is no trailing slash?

https://github.com/llvm/llvm-project/pull/74319


More information about the llvm-commits mailing list