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

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 7 12:16:56 PST 2023


Author: Ilia Kuklin
Date: 2023-12-08T01:16:52+05:00
New Revision: bdcb841aa729eabc03896c74c6ddfbf836356d77

URL: https://github.com/llvm/llvm-project/commit/bdcb841aa729eabc03896c74c6ddfbf836356d77
DIFF: https://github.com/llvm/llvm-project/commit/bdcb841aa729eabc03896c74c6ddfbf836356d77.diff

LOG: [objcopy] Return an error in case of an invalid regex (#74319)

As of now, llvm-objcopy silently ignores a provided regex if it doesn't
compile.

This patch adds returning an error saying that a regex couldn't be
compiled, along with the compilation error message.

---------

Co-authored-by: James Henderson <46713263+jh7370 at users.noreply.github.com>

Added: 
    llvm/test/tools/llvm-objcopy/regex-error.test

Modified: 
    llvm/lib/ObjCopy/CommonConfig.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ObjCopy/CommonConfig.cpp b/llvm/lib/ObjCopy/CommonConfig.cpp
index e85715d0c44cb3..f44e70d996b2e1 100644
--- a/llvm/lib/ObjCopy/CommonConfig.cpp
+++ b/llvm/lib/ObjCopy/CommonConfig.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ObjCopy/CommonConfig.h"
+#include "llvm/Support/Errc.h"
 
 namespace llvm {
 namespace objcopy {
@@ -38,6 +39,12 @@ NameOrPattern::create(StringRef Pattern, MatchStyle MS,
                          IsPositiveMatch);
   }
   case MatchStyle::Regex: {
+    Regex RegEx(Pattern);
+    std::string Err;
+    if (!RegEx.isValid(Err))
+      return createStringError(errc::invalid_argument,
+                               "cannot compile regular expression \'" +
+                                   Pattern + "\': " + Err);
     SmallVector<char, 32> Data;
     return NameOrPattern(std::make_shared<Regex>(
         ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)));

diff  --git a/llvm/test/tools/llvm-objcopy/regex-error.test b/llvm/test/tools/llvm-objcopy/regex-error.test
new file mode 100644
index 00000000000000..ca9bb2f3b4f9f8
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/regex-error.test
@@ -0,0 +1,13 @@
+## Test if providing objcopy with an invalid regex generates an error.
+
+# RUN: yaml2obj %s -o %t
+
+# RUN: not llvm-objcopy --regex --strip-symbol='[^)\' %t /dev/null 2>&1 | FileCheck %s
+# CHECK: error: cannot compile regular expression '[^)\'
+
+!ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64


        


More information about the llvm-commits mailing list