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

Ilia Kuklin via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 7 03:08:47 PST 2023


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

>From 9c14c0a462d3d046ede5de54f7c55317b02293ab Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Mon, 4 Dec 2023 18:19:29 +0500
Subject: [PATCH 1/3] [objcopy] Return an error in case of an invalid regex

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.
---
 llvm/lib/ObjCopy/CommonConfig.cpp             | 12 +++++++++--
 llvm/test/tools/llvm-objcopy/regex-error.test | 20 +++++++++++++++++++
 2 files changed, 30 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/tools/llvm-objcopy/regex-error.test

diff --git a/llvm/lib/ObjCopy/CommonConfig.cpp b/llvm/lib/ObjCopy/CommonConfig.cpp
index e85715d0c44cb..9da0724d8ee6f 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 {
@@ -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))
+      return createStringError(errc::invalid_argument,
+                               "cannot compile regular expression \'" +
+                                   Pattern + "\': " + Err);
+    return NameOrPattern(RegEx);
   }
   }
   llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum");
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 0000000000000..b6c14ba19690b
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/regex-error.test
@@ -0,0 +1,20 @@
+## Test if providing objcopy with an invalid regex generates an error.
+
+# RUN: yaml2obj %s -o %t
+
+# RUN: not llvm-objcopy --regex --strip-symbol='f[^)' %t /dev/null 2>&1 | FileCheck %s
+# CHECK: cannot compile regular expression
+
+!ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name:  .text
+    Type:  SHT_PROGBITS
+    Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+Symbols:
+  - Name:    foo
+    Section: .text

>From 25835346523ad6455492dc1f4ef854106dc75c15 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Wed, 6 Dec 2023 23:04:27 +0500
Subject: [PATCH 2/3] Moved the error check before anchoring; fixed up the
 test.

---
 llvm/lib/ObjCopy/CommonConfig.cpp             | 11 +++++------
 llvm/test/tools/llvm-objcopy/regex-error.test | 11 ++---------
 2 files changed, 7 insertions(+), 15 deletions(-)

diff --git a/llvm/lib/ObjCopy/CommonConfig.cpp b/llvm/lib/ObjCopy/CommonConfig.cpp
index 9da0724d8ee6f..deadb3f2ce958 100644
--- a/llvm/lib/ObjCopy/CommonConfig.cpp
+++ b/llvm/lib/ObjCopy/CommonConfig.cpp
@@ -39,16 +39,15 @@ NameOrPattern::create(StringRef Pattern, MatchStyle MS,
                          IsPositiveMatch);
   }
   case MatchStyle::Regex: {
-    SmallVector<char, 32> Data;
-    auto AnchoredPattern =
-        ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data);
-    auto RegEx = std::make_shared<Regex>(AnchoredPattern);
+    auto RegEx = Regex(Pattern);
     std::string Err;
-    if (!RegEx->isValid(Err))
+    if (!RegEx.isValid(Err))
       return createStringError(errc::invalid_argument,
                                "cannot compile regular expression \'" +
                                    Pattern + "\': " + Err);
-    return NameOrPattern(RegEx);
+    SmallVector<char, 32> Data;
+    return NameOrPattern(std::make_shared<Regex>(
+        ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)));
   }
   }
   llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum");
diff --git a/llvm/test/tools/llvm-objcopy/regex-error.test b/llvm/test/tools/llvm-objcopy/regex-error.test
index b6c14ba19690b..ca9bb2f3b4f9f 100644
--- a/llvm/test/tools/llvm-objcopy/regex-error.test
+++ b/llvm/test/tools/llvm-objcopy/regex-error.test
@@ -2,8 +2,8 @@
 
 # RUN: yaml2obj %s -o %t
 
-# RUN: not llvm-objcopy --regex --strip-symbol='f[^)' %t /dev/null 2>&1 | FileCheck %s
-# CHECK: cannot compile regular expression
+# RUN: not llvm-objcopy --regex --strip-symbol='[^)\' %t /dev/null 2>&1 | FileCheck %s
+# CHECK: error: cannot compile regular expression '[^)\'
 
 !ELF
 FileHeader:
@@ -11,10 +11,3 @@ FileHeader:
   Data:    ELFDATA2LSB
   Type:    ET_EXEC
   Machine: EM_X86_64
-Sections:
-  - Name:  .text
-    Type:  SHT_PROGBITS
-    Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
-Symbols:
-  - Name:    foo
-    Section: .text

>From e02e0a9ae49c7b69237d281c0cd0a004644dd74e Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <kuklin.iy at mail.ru>
Date: Thu, 7 Dec 2023 16:08:40 +0500
Subject: [PATCH 3/3] Changed RegEx initialization style

Co-authored-by: James Henderson <46713263+jh7370 at users.noreply.github.com>
---
 llvm/lib/ObjCopy/CommonConfig.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/ObjCopy/CommonConfig.cpp b/llvm/lib/ObjCopy/CommonConfig.cpp
index deadb3f2ce958..f44e70d996b2e 100644
--- a/llvm/lib/ObjCopy/CommonConfig.cpp
+++ b/llvm/lib/ObjCopy/CommonConfig.cpp
@@ -39,7 +39,7 @@ NameOrPattern::create(StringRef Pattern, MatchStyle MS,
                          IsPositiveMatch);
   }
   case MatchStyle::Regex: {
-    auto RegEx = Regex(Pattern);
+    Regex RegEx(Pattern);
     std::string Err;
     if (!RegEx.isValid(Err))
       return createStringError(errc::invalid_argument,



More information about the llvm-commits mailing list