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

Ilia Kuklin via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 4 05:36:57 PST 2023


https://github.com/kuilpd created https://github.com/llvm/llvm-project/pull/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.

>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] [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



More information about the llvm-commits mailing list