aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlia Kuklin <kuklin.iy@mail.ru>2023-12-08 01:16:52 +0500
committerGitHub <noreply@github.com>2023-12-08 01:16:52 +0500
commitbdcb841aa729eabc03896c74c6ddfbf836356d77 (patch)
treece438c8c51d8f1202bf1bf6cba27629b00ea8af9
parenta4d4b45aef6dbac1cead60dcba5e60939fc1656d (diff)
downloadllvm-bdcb841aa729eabc03896c74c6ddfbf836356d77.zip
llvm-bdcb841aa729eabc03896c74c6ddfbf836356d77.tar.gz
llvm-bdcb841aa729eabc03896c74c6ddfbf836356d77.tar.bz2
[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@users.noreply.github.com>
-rw-r--r--llvm/lib/ObjCopy/CommonConfig.cpp7
-rw-r--r--llvm/test/tools/llvm-objcopy/regex-error.test13
2 files changed, 20 insertions, 0 deletions
diff --git a/llvm/lib/ObjCopy/CommonConfig.cpp b/llvm/lib/ObjCopy/CommonConfig.cpp
index e85715d..f44e70d 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 0000000..ca9bb2f
--- /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