Skip to content

Commit bdcb841

Browse files
kuilpdjh7370
andauthored
[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 <[email protected]>
1 parent a4d4b45 commit bdcb841

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

llvm/lib/ObjCopy/CommonConfig.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/ObjCopy/CommonConfig.h"
10+
#include "llvm/Support/Errc.h"
1011

1112
namespace llvm {
1213
namespace objcopy {
@@ -38,6 +39,12 @@ NameOrPattern::create(StringRef Pattern, MatchStyle MS,
3839
IsPositiveMatch);
3940
}
4041
case MatchStyle::Regex: {
42+
Regex RegEx(Pattern);
43+
std::string Err;
44+
if (!RegEx.isValid(Err))
45+
return createStringError(errc::invalid_argument,
46+
"cannot compile regular expression \'" +
47+
Pattern + "\': " + Err);
4148
SmallVector<char, 32> Data;
4249
return NameOrPattern(std::make_shared<Regex>(
4350
("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data)));
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Test if providing objcopy with an invalid regex generates an error.
2+
3+
# RUN: yaml2obj %s -o %t
4+
5+
# RUN: not llvm-objcopy --regex --strip-symbol='[^)\' %t /dev/null 2>&1 | FileCheck %s
6+
# CHECK: error: cannot compile regular expression '[^)\'
7+
8+
!ELF
9+
FileHeader:
10+
Class: ELFCLASS64
11+
Data: ELFDATA2LSB
12+
Type: ET_EXEC
13+
Machine: EM_X86_64

0 commit comments

Comments
 (0)