-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[objcopy] Return an error in case of an invalid regex #74319
Conversation
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/pr-subscribers-llvm-binary-utilities Author: Ilia Kuklin (kuilpd) ChangesAs 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:
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
|
This LGTM. Please wait a couple of days if @jh7370 has any issue. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the commit/PR title's tag should be llvm-objcopy, not simply objcopy.
Overall, the patch looks fine. Just a few small comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, with nit
Co-authored-by: James Henderson <46713263+jh7370@users.noreply.github.com>
This comment was not resolved. The subject is "[objcopy] Return an error in case of an invalid regex #74319". Don't forget it for future contributions:) |
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.