Skip to content
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

Merged
merged 3 commits into from
Dec 7, 2023

Conversation

kuilpd
Copy link
Contributor

@kuilpd kuilpd commented Dec 4, 2023

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.

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.
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 4, 2023

@llvm/pr-subscribers-llvm-binary-utilities

Author: Ilia Kuklin (kuilpd)

Changes

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

@avl-llvm avl-llvm requested a review from jh7370 December 4, 2023 15:38
@avl-llvm
Copy link
Collaborator

avl-llvm commented Dec 4, 2023

This LGTM. Please wait a couple of days if @jh7370 has any issue.

Copy link
Collaborator

@avl-llvm avl-llvm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@jh7370 jh7370 left a 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.

llvm/test/tools/llvm-objcopy/regex-error.test Outdated Show resolved Hide resolved
llvm/test/tools/llvm-objcopy/regex-error.test Show resolved Hide resolved
llvm/lib/ObjCopy/CommonConfig.cpp Outdated Show resolved Hide resolved
Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with nit

llvm/lib/ObjCopy/CommonConfig.cpp Outdated Show resolved Hide resolved
Co-authored-by: James Henderson <46713263+jh7370@users.noreply.github.com>
@kuilpd kuilpd merged commit bdcb841 into llvm:main Dec 7, 2023
4 checks passed
@MaskRay
Copy link
Member

MaskRay commented Dec 7, 2023

Nit: the commit/PR title's tag should be llvm-objcopy, not simply objcopy.

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:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants