[llvm] [objcopy] Return an error in case of an invalid regex (PR #74319)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 4 05:37:24 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-binary-utilities
Author: Ilia Kuklin (kuilpd)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/74319.diff
2 Files Affected:
- (modified) llvm/lib/ObjCopy/CommonConfig.cpp (+10-2)
- (added) llvm/test/tools/llvm-objcopy/regex-error.test (+20)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/74319
More information about the llvm-commits
mailing list